private static async Task <MqttBasePacket> DeserializeConnectAsync(MqttPacketReader reader)
        {
            await reader.ReadRemainingDataAsync(2); // Skip 2 bytes

            var protocolName = await reader.ReadRemainingDataAsync(4);

            if (Encoding.UTF8.GetString(protocolName, 0, protocolName.Length) != "MQTT")
            {
                throw new MqttProtocolViolationException("Protocol name is not 'MQTT'.");
            }

            var protocolLevel = await reader.ReadRemainingDataByteAsync();

            var connectFlags = await reader.ReadRemainingDataByteAsync();

            var connectFlagsReader = new ByteReader(connectFlags);

            connectFlagsReader.Read(); // Reserved.

            var packet = new MqttConnectPacket
            {
                CleanSession = connectFlagsReader.Read()
            };

            var willFlag     = connectFlagsReader.Read();
            var willQoS      = connectFlagsReader.Read(2);
            var willRetain   = connectFlagsReader.Read();
            var passwordFlag = connectFlagsReader.Read();
            var usernameFlag = connectFlagsReader.Read();

            packet.KeepAlivePeriod = await reader.ReadRemainingDataUShortAsync();

            packet.ClientId = await reader.ReadRemainingDataStringWithLengthPrefixAsync();

            if (willFlag)
            {
                packet.WillMessage = new MqttApplicationMessage(
                    await reader.ReadRemainingDataStringWithLengthPrefixAsync(),
                    await reader.ReadRemainingDataWithLengthPrefixAsync(),
                    (MqttQualityOfServiceLevel)willQoS,
                    willRetain);
            }

            if (usernameFlag)
            {
                packet.Username = await reader.ReadRemainingDataStringWithLengthPrefixAsync();
            }

            if (passwordFlag)
            {
                packet.Password = await reader.ReadRemainingDataStringWithLengthPrefixAsync();
            }

            ValidateConnectPacket(packet);
            return(packet);
        }