TcpListener: ¿cómo dejar de escuchar mientras se espera AcceptTcpClientAsync ()?

No sé cómo cerrar correctamente un TcpListener mientras un método asíncrono espera las conexiones entrantes. Encontré este código en SO, aquí el código:

public class Server
{
    private TcpListener _Server;
    private bool _Active;

    public Server()
    {
        _Server = new TcpListener(IPAddress.Any, 5555);
    }

    public async void StartListening()
    {
        _Active = true;
        _Server.Start();
        await AcceptConnections();
    }

    public void StopListening()
    {
        _Active = false;
        _Server.Stop();
    }

    private async Task AcceptConnections()
    {
        while (_Active)
        {
            var client = await _Server.AcceptTcpClientAsync();
            DoStuffWithClient(client);
        }
    }

    private void DoStuffWithClient(TcpClient client)
    {
        // ...
    }

}

Y el principal:

    static void Main(string[] args)
    {
        var server = new Server();
        server.StartListening();

        Thread.Sleep(5000);

        server.StopListening();
        Console.Read();
    }

Se lanza una excepción en esta línea.

        await AcceptConnections();

cuando llamo Server.StopListening (), el objeto se elimina.

Entonces, mi pregunta es, ¿cómo puedo cancelar AcceptTcpClientAsync () para cerrar TcpListener correctamente?

Respuestas a la pregunta(7)

Su respuesta a la pregunta