public async Task RunAsync() { _listener.Start(); while (true) { TcpClient tcpClient = null; try { tcpClient = await _listener.AcceptTcpClientAsync(); } catch (ObjectDisposedException) { // When TcpListener is stopped, outstanding calls to AcceptTcpClientAsync // will throw an ObjectDisposedException. When this happens, it's time to // exit. return; } var connection = new Connection(tcpClient, _cancellationTokenSource.Token); var session = _sessionFactory(); var sessionTask = session.HandleConnection(connection); SessionManager.IncreaseSessionCount(session.Protocol); HandleSessionAsynchronously(sessionTask, connection, session); } }