public static StunMessage Parse(byte[] bytes) { var bits = new Bits(bytes); if (bits.Pop() != false || bits.Pop() != false) { throw new Exception("First two bits must be zeroes."); } var type = StunMessageType.Parse(bits.PopBits(14)); var length = BitConverter.ToUInt16(bits.PopLittleEndianBytes(2), 0); var magicCookie = BitConverter.ToUInt32(bits.PopLittleEndianBytes(4), 0); if (magicCookie != MagicCookie) { throw new ArgumentException($"The parse magic cookie {magicCookie} doesn't match {MagicCookie}.", nameof(MagicCookie)); } var attributes = new List<StunMessageAttribute>(); for (var index = 0; index < length; index++) { attributes.Add(StunMessageAttribute.Parse(bits.PopBits(12 * 8))); } var id = StunMessageId.Parse(bits.PopLittleEndianBytes(12)); return new StunMessage(type, attributes, id); }