/// <summary>
        /// Decode the payload for type of Referral Entry V3V4_NameListReferral
        /// </summary>
        /// <param name="channel">channel with payload data</param>
        /// <exception cref=" System.InvalidOperationException">
        /// Fail to decode payload data to ReferralV3V4_NameListReferral structure
        /// </exception>
        private void DecodeReferralV3V4_NameListReferral(Channel channel)
        {
            bool isDfsPathFollowed = false;
            List<DFS_REFERRAL_V3V4_NameListReferral> tempList = new List<DFS_REFERRAL_V3V4_NameListReferral>();
            // The total size of all fields in front of Padding field in DFS_REFERRAL_V3V4_NameListReferral
            const ushort referral3FixedSize = 18;

            for (int i = 0; i < this.referralResponse.NumberOfReferrals; i++)
            {
                DFS_REFERRAL_V3V4_NameListReferral referral3 = new DFS_REFERRAL_V3V4_NameListReferral();

                // Read the fixed portion of DFS_REFERRAL_V3V4_NameListReferral
                referral3.VersionNumber = channel.Read<ushort>();
                referral3.Size = channel.Read<ushort>();
                referral3.ServerType = channel.Read<ushort>();
                referral3.ReferralEntryFlags = channel.Read<ReferralEntryFlags_Values>();
                referral3.TimeToLive = channel.Read<uint>();
                referral3.SpecialNameOffset = channel.Read<ushort>();
                referral3.NumberOfExpandedNames = channel.Read<ushort>();
                referral3.ExpandedNameOffset = channel.Read<ushort>();
                referral3.Padding = channel.ReadBytes(referral3.Size - referral3FixedSize);

                if (i == 0 && referral3.SpecialNameOffset == referral3.Size)
                {
                    isDfsPathFollowed = true;
                }

                // The Dfs paths of immediately follows each referral entry. Read the paths orderly.
                if (isDfsPathFollowed)
                {
                    // Get the Dfs paths from channel
                    // TD does not mention whether padding data exists between Dfs paths.
                    // Drop the possibly exists padding data
                    referral3.SpecialName = ReadDfsPath(channel);
                    ReadPadding(channel, referral3.ExpandedNameOffset - referral3.SpecialNameOffset
                        - (referral3.SpecialName.Length + 1) * sizeofWord);
                    referral3.DCNameArray = new string[referral3.NumberOfExpandedNames];

                    for (int j = 0; j < referral3.NumberOfExpandedNames; j++)
                    {
                        referral3.DCNameArray[j] = ReadDfsPath(channel);
                    }
                }
                tempList.Add(referral3);
            }

            // All Dfs paths follow the last referral entry.Read the paths after all entries have been read.
            if (!isDfsPathFollowed)
            {
                ushort referralFixedSize = tempList[0].Size;
                byte[] pathData = channel.ReadBytes((int)(channel.Stream.Length - channel.Stream.Position));

                for (int i = 0; i < tempList.Count; i++)
                {
                    // Calculate the offsets of Dfs paths
                    int leftCount = tempList.Count - i;
                    DFS_REFERRAL_V3V4_NameListReferral referral3 = tempList[i];
                    int specialNameOffset = referral3.SpecialNameOffset - leftCount * referralFixedSize;
                    int dcNameOffset = referral3.ExpandedNameOffset - leftCount * referralFixedSize;

                    // Get the Dfs paths
                    referral3.SpecialName = ReadDfsPath(pathData, specialNameOffset);
                    referral3.DCNameArray = new string[referral3.NumberOfExpandedNames];

                    for (int j = 0; j < referral3.NumberOfExpandedNames; j++)
                    {
                        referral3.DCNameArray[j] = ReadDfsPath(pathData, dcNameOffset);
                        dcNameOffset += referral3.DCNameArray[j].Length * sizeofWord;
                    }
                    tempList[i] = referral3;
                }
            }
            this.referralResponse.ReferralEntries = tempList.ToArray();
        }
        /// <summary>
        /// Decode the payload for type of Referral Entry V3V4_NameListReferral
        /// </summary>
        /// <param name="channel">channel with payload data</param>
        /// <exception cref=" System.InvalidOperationException">
        /// Fail to decode payload data to ReferralV3V4_NameListReferral structure
        /// </exception>
        private void DecodeReferralV3V4_NameListReferral(Channel channel)
        {
            bool isDfsPathFollowed = false;
            List <DFS_REFERRAL_V3V4_NameListReferral> tempList = new List <DFS_REFERRAL_V3V4_NameListReferral>();
            // The total size of all fields in front of Padding field in DFS_REFERRAL_V3V4_NameListReferral
            const ushort referral3FixedSize = 18;

            for (int i = 0; i < this.referralResponse.NumberOfReferrals; i++)
            {
                DFS_REFERRAL_V3V4_NameListReferral referral3 = new DFS_REFERRAL_V3V4_NameListReferral();

                // Read the fixed portion of DFS_REFERRAL_V3V4_NameListReferral
                referral3.VersionNumber         = channel.Read <ushort>();
                referral3.Size                  = channel.Read <ushort>();
                referral3.ServerType            = channel.Read <ushort>();
                referral3.ReferralEntryFlags    = channel.Read <ReferralEntryFlags_Values>();
                referral3.TimeToLive            = channel.Read <uint>();
                referral3.SpecialNameOffset     = channel.Read <ushort>();
                referral3.NumberOfExpandedNames = channel.Read <ushort>();
                referral3.ExpandedNameOffset    = channel.Read <ushort>();
                referral3.Padding               = channel.ReadBytes(referral3.Size - referral3FixedSize);

                if (i == 0 && referral3.SpecialNameOffset == referral3.Size)
                {
                    isDfsPathFollowed = true;
                }

                // The Dfs paths of immediately follows each referral entry. Read the paths orderly.
                if (isDfsPathFollowed)
                {
                    // Get the Dfs paths from channel
                    // TD does not mention whether padding data exists between Dfs paths.
                    // Drop the possibly exists padding data
                    referral3.SpecialName = ReadDfsPath(channel);
                    ReadPadding(channel, referral3.ExpandedNameOffset - referral3.SpecialNameOffset
                                - (referral3.SpecialName.Length + 1) * sizeofWord);
                    referral3.DCNameArray = new string[referral3.NumberOfExpandedNames];

                    for (int j = 0; j < referral3.NumberOfExpandedNames; j++)
                    {
                        referral3.DCNameArray[j] = ReadDfsPath(channel);
                    }
                }
                tempList.Add(referral3);
            }

            // All Dfs paths follow the last referral entry.Read the paths after all entries have been read.
            if (!isDfsPathFollowed)
            {
                ushort referralFixedSize = tempList[0].Size;
                byte[] pathData          = channel.ReadBytes((int)(channel.Stream.Length - channel.Stream.Position));

                for (int i = 0; i < tempList.Count; i++)
                {
                    // Calculate the offsets of Dfs paths
                    int leftCount = tempList.Count - i;
                    DFS_REFERRAL_V3V4_NameListReferral referral3 = tempList[i];
                    int specialNameOffset = referral3.SpecialNameOffset - leftCount * referralFixedSize;
                    int dcNameOffset      = referral3.ExpandedNameOffset - leftCount * referralFixedSize;

                    // Get the Dfs paths
                    referral3.SpecialName = ReadDfsPath(pathData, specialNameOffset);
                    referral3.DCNameArray = new string[referral3.NumberOfExpandedNames];

                    for (int j = 0; j < referral3.NumberOfExpandedNames; j++)
                    {
                        referral3.DCNameArray[j] = ReadDfsPath(pathData, dcNameOffset);
                        dcNameOffset            += referral3.DCNameArray[j].Length * sizeofWord;
                    }
                    tempList[i] = referral3;
                }
            }
            this.referralResponse.ReferralEntries = tempList.ToArray();
        }