private async Task ProcessTcpClientAsync(TcpClient tcpClient) { try { Console.WriteLine("Connection opened. Creating new session"); if (await AcceptWebSocketSession(tcpClient)) { Guid guid = Guid.NewGuid(); WebSocketSession webSocketSession = new WebSocketSession(tcpClient, ref OnMessage, guid); _sessions.Add(guid, webSocketSession); Console.WriteLine("Sessions : " + _sessions.Count); await webSocketSession.Process(); //Infinite loop _sessions.Remove(guid); //Remove session from list and closing stream and tcpClient CloseSession(webSocketSession); } else { Console.WriteLine("Accepting failed. Closing connection"); tcpClient.Close(); } } catch (Exception ex) { Console.WriteLine("Error occured while creating new connection"); } }