private void OnClientConnected(TcpListener server, IAsyncResult r, CancellationToken token) { if (token.IsCancellationRequested) { return; } // Create and add client item TcpClient client = server.EndAcceptTcpClient(r); var clientItem = new ClientItemTCP() { Id = ++_lastId, Client = client }; _clientListener.AddClient(clientItem); // Get and send on connected message var packet = new Packet(_manager.Connected(clientItem)); packet.WriteOnStream(client.GetStream()); // Accept next client server.BeginAcceptTcpClient((result) => OnClientConnected(server, result, token), null); }