title: Cancel listening Task upon closing its associated socket
body: <p>I have a TcpListener, which can be started</p>
  <pre class="lang-cs prettyprint-override"><code>public void Start()
  {
      if (!Listening &amp;&amp; !IsDisposed)
      {
          _socket.Bind(_localEndpoint);
          Task.Run(DoListen);
      }
  }
  
  private async Task DoListen()
  {
      _socket.Listen();
      Listening = true;
      try
      {
          while (Listening)
          {
              var socket = await _socket.AcceptAsync().ConfigureAwait(false);
              NewConnection?.Invoke(new Connection(socket));
          }
      }
      catch (SocketException e)
      {
          Stop();
      }
      finally
      {
          Listening = false;
      }
  }
  </code></pre>
  <p>and stopped</p>
  <pre class="lang-cs prettyprint-override"><code>public void Stop()
  {
      _socket?.Close();
      IsDisposed = true;
      Listening = false;
  }
  </code></pre>
  <p>How can I cancel the Task started by calling <code>Start()</code>?<br />
  By making the loops condition <code>Listening</code>, I can ensure that the task will stop when the condition is evaluated.<br />
  However, this would only happen, after a new connection is accepted.<br />
  How can I get out of awaiting <code>var socket = await _socket.AcceptAsync().ConfigureAwait(false);</code>?
  What happends if the associated socket is closed whilst awaiting <code>AcceptAsync()</code>?
  Thanks for your answers!</p>