public static object ReadObj(this Socket socket, Type type, int payloadSize) { if (socket == null) { throw new ArgumentNullException("socket"); } if (type == null) { throw new ArgumentNullException("type"); } using (var networkStream = new NetworkStream(socket, false)) using (var memoryStream = new MemoryStream()) { networkStream.CopyBufferedLimited(memoryStream, payloadSize); string incoming = new ASCIIEncoding().GetString(memoryStream.ToArray()); return(incoming.FromJson(type)); } }