private void Receive(object state) { try { string dataReceive = string.Empty; byte[] bytesReceived = new byte[512]; int bytes = 0; do { bytes = tempClient.Receive(bytesReceived, bytesReceived.Length, 0); dataReceive = dataReceive + Encoding.ASCII.GetString(bytesReceived, 0, bytes); if (dataReceive.EndsWith("\n") && dataReceive.StartsWith("connect:")) { Send("ok:1\n"); IsClientConnected = true; ClientConnected?.Invoke(); } else if (dataReceive == "close\n") { tempClient.Close(); IsClientConnected = false; return; } else if (dataReceive.StartsWith(":")) { ClientCommand?.Invoke(dataReceive); } dataReceive = string.Empty; }while (bytes > 0); } catch (SocketException e) { IsClientConnected = false; Console.WriteLine("Client disconnected..."); } }