public void read() { List <byte> allTheBytes = new List <byte>(); while (true) { byte[] buffer = new byte[512]; int received = client.GetStream().Read(buffer, 0, buffer.Length); if (received > 0) { byte[] rawData = new byte[received]; Array.Copy(buffer, 0, rawData, 0, received); allTheBytes = allTheBytes.Concat(rawData).ToList(); int packetSize = Packet.getLengthOfPacket(allTheBytes); if (packetSize == -1) { return; } JObject json = Packet.RetrieveJSON(packetSize, ref allTheBytes); if (json != null && OnReceivedJSON != null) { OnReceivedJSON(json); } } } }