void listenclient(TcpClient client) { StringBuilder builder; NetworkStream stream = client.GetStream(); int i = 0; try { while (true) { builder = new StringBuilder(); int bytes = 0; byte[] data = new byte[1000000]; i = 1; do { bytes = stream.Read(data, 0, data.Length); builder.Append(Encoding.Unicode.GetString(data, 0, bytes)); }while (client.Available > 0); string newstate = ExtractLastMessage(data, bytes); GameState state = JsonConvert.DeserializeObject <GameState>(newstate); owner.Invoke(new Del((s) => owner.LoadFromState(s)), state); } } catch (Exception ex) { owner.Invoke(new DelNull(() => owner.ResetGame())); client.Close(); } }