public bool TryGetPacket(out Message p) { p = null; var result = false; if (DataStream.Count() >= 5) { var length = (0x00 << 24) | (DataStream[2] << 16) | (DataStream[3] << 8) | DataStream[4]; var type = (ushort)((DataStream[0] << 8) | DataStream[1]); if (DataStream.Count - 7 >= length) { object obj = null; var packet = DataStream.Take(7 + length).ToArray(); using (var br = new BinaryReader(new MemoryStream(packet))) obj = MessageFactory.Read(this, br, type); if (obj != null) { p = (Message)obj; result = true; } else { //Update Decryption Key var data = DataStream.Skip(7).Take(length).ToArray(); Decrypt(data); } DataStream.RemoveRange(0, 7 + length); } } return(result); }