public async Task Process() { await Task.Run(() => { try { Stream = client.GetStream(); var command = GetMessage(); server.Handler.HandlerRequest(command, this); Console.WriteLine("Entered the program"); // в бесконечном цикле получаем сообщения от клиента while (true) { try { command = GetMessage(); server.Handler.HandlerRequest(command, this); } catch { Console.WriteLine("Left the program"); break; } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { // в случае выхода из цикла закрываем ресурсы server.RemoveConnection(this.Id); Close(); } }); }