/// <summary>
        /// Decode the payload data from byte array to RESP_GET_DFS_REFERRAL structure.
        /// And assign the result to referralResponse field.
        /// </summary>
        /// <param name="payloadData">RESP_GET_DFS_REFERRAL in byte array</param>
        /// <exception cref=" System.InvalidOperationException">
        /// Fail to decode payload data to RESP_GET_DFS_REFERRAL structure
        /// </exception>
        /// <exception cref=" System.FormatException">payloadData is null or empty</exception>
        private void FromBytes(byte[] payloadData)
        {
            if (payloadData != null && payloadData.Length > 0)
            {
                using (MemoryStream memoryStream = new MemoryStream(payloadData))
                {
                    Channel channel = new Channel(null, memoryStream);

                    // Read the referral header
                    this.referralResponse.PathConsumed = channel.Read<ushort>();
                    this.referralResponse.NumberOfReferrals = channel.Read<ushort>();
                    this.referralResponse.ReferralHeaderFlags = channel.Read<ReferralHeaderFlags>();

                    // Read Referral Entries
                    if (this.referralResponse.NumberOfReferrals > 0)
                    {
                        this.versionNumber = channel.Peek<ushort>(0);

                        switch (this.versionNumber)
                        {
                            case 0x0001:
                                List<DFS_REFERRAL_V1> referral1List = new List<DFS_REFERRAL_V1>();
                                // The total size of VersionNumber, Size, ServerType and ReferralEntryFlags in DFS_REFERRAL_V1
                                const ushort referralV1fixedSize = 8;

                                for (int i = 0; i < this.referralResponse.NumberOfReferrals; i++)
                                {
                                    DFS_REFERRAL_V1 referral1 = new DFS_REFERRAL_V1();
                                    referral1.VersionNumber = channel.Read<ushort>();
                                    referral1.Size = channel.Read<ushort>();
                                    referral1.ServerType = channel.Read<ushort>();
                                    referral1.ReferralEntryFlags = channel.Read<ushort>();
                                    referral1.ShareName = Encoding.Unicode.GetString(channel.ReadBytes(referral1.Size
                                        - referralV1fixedSize - sizeofWord));
                                    channel.ReadBytes(sizeofWord);
                                    referral1List.Add(referral1);
                                }
                                this.referralResponse.ReferralEntries = referral1List.ToArray();
                                break;

                            case 0x0002:
                                DecodeReferralV2(channel);
                                break;

                            case 0x0003:
                            case 0x0004:
                                // The offset from beginning of DFS_REFERRAL_V3V4 to ReferralEntryFlags field
                                ushort flagOffset = 6;
                                ReferralEntryFlags_Values referralEntryFlags = channel.Peek<ReferralEntryFlags_Values>(flagOffset);

                                if ((referralEntryFlags & ReferralEntryFlags_Values.NameListReferral)
                                    == ReferralEntryFlags_Values.NameListReferral)
                                {
                                    this.isNameListReferral = true;
                                    DecodeReferralV3V4_NameListReferral(channel);
                                }
                                else
                                {
                                    this.isNameListReferral = false;
                                    DecodeReferralV3V4_NonNameListReferral(channel);
                                }
                                break;

                            default:
                                throw new InvalidOperationException("The version number of Referral Entry is not correct.");
                        }
                    }
                }
            }
            else
            {
                throw new FormatException("payload byte array is null or empty for decoding.");
            }
        }
        /// <summary>
        /// Decode the payload data from byte array to RESP_GET_DFS_REFERRAL structure.
        /// And assign the result to referralResponse field.
        /// </summary>
        /// <param name="payloadData">RESP_GET_DFS_REFERRAL in byte array</param>
        /// <exception cref=" System.InvalidOperationException">
        /// Fail to decode payload data to RESP_GET_DFS_REFERRAL structure
        /// </exception>
        /// <exception cref=" System.FormatException">payloadData is null or empty</exception>
        private void FromBytes(byte[] payloadData)
        {
            if (payloadData != null && payloadData.Length > 0)
            {
                using (MemoryStream memoryStream = new MemoryStream(payloadData))
                {
                    Channel channel = new Channel(null, memoryStream);

                    // Read the referral header
                    this.referralResponse.PathConsumed        = channel.Read <ushort>();
                    this.referralResponse.NumberOfReferrals   = channel.Read <ushort>();
                    this.referralResponse.ReferralHeaderFlags = channel.Read <ReferralHeaderFlags>();

                    // Read Referral Entries
                    if (this.referralResponse.NumberOfReferrals > 0)
                    {
                        this.versionNumber = channel.Peek <ushort>(0);

                        switch (this.versionNumber)
                        {
                        case 0x0001:
                            List <DFS_REFERRAL_V1> referral1List = new List <DFS_REFERRAL_V1>();
                            // The total size of VersionNumber, Size, ServerType and ReferralEntryFlags in DFS_REFERRAL_V1
                            const ushort referralV1fixedSize = 8;

                            for (int i = 0; i < this.referralResponse.NumberOfReferrals; i++)
                            {
                                DFS_REFERRAL_V1 referral1 = new DFS_REFERRAL_V1();
                                referral1.VersionNumber      = channel.Read <ushort>();
                                referral1.Size               = channel.Read <ushort>();
                                referral1.ServerType         = channel.Read <ushort>();
                                referral1.ReferralEntryFlags = channel.Read <ushort>();
                                referral1.ShareName          = Encoding.Unicode.GetString(channel.ReadBytes(referral1.Size
                                                                                                            - referralV1fixedSize - sizeofWord));
                                channel.ReadBytes(sizeofWord);
                                referral1List.Add(referral1);
                            }
                            this.referralResponse.ReferralEntries = referral1List.ToArray();
                            break;

                        case 0x0002:
                            DecodeReferralV2(channel);
                            break;

                        case 0x0003:
                        case 0x0004:
                            // The offset from beginning of DFS_REFERRAL_V3V4 to ReferralEntryFlags field
                            ushort flagOffset = 6;
                            ReferralEntryFlags_Values referralEntryFlags = channel.Peek <ReferralEntryFlags_Values>(flagOffset);

                            if ((referralEntryFlags & ReferralEntryFlags_Values.NameListReferral)
                                == ReferralEntryFlags_Values.NameListReferral)
                            {
                                this.isNameListReferral = true;
                                DecodeReferralV3V4_NameListReferral(channel);
                            }
                            else
                            {
                                this.isNameListReferral = false;
                                DecodeReferralV3V4_NonNameListReferral(channel);
                            }
                            break;

                        default:
                            throw new InvalidOperationException("The version number of Referral Entry is not correct.");
                        }
                    }
                }
            }
            else
            {
                throw new FormatException("payload byte array is null or empty for decoding.");
            }
        }