/// <summary>
        /// Decode the payload for type of Referral Entry 2
        /// </summary>
        /// <param name="channel">channel with payload data</param>
        /// <exception cref=" System.InvalidOperationException">
        /// Fail to decode payload data to ReferralV2 structure
        /// </exception>
        private void DecodeReferralV2(Channel channel)
        {
            bool isDfsPathFollowed = false;
            List<DFS_REFERRAL_V2> referral2List = new List<DFS_REFERRAL_V2>();

            for (int i = 0; i < this.referralResponse.NumberOfReferrals; i++)
            {
                DFS_REFERRAL_V2 referral2 = new DFS_REFERRAL_V2();
                // Read the fixed portion of DFS_REFERRAL_V2
                referral2.VersionNumber = channel.Read<ushort>();
                referral2.Size = channel.Read<ushort>();
                referral2.ServerType = channel.Read<ushort>();
                referral2.ReferralEntryFlags = channel.Read<ushort>();
                referral2.Proximity = channel.Read<uint>();
                referral2.TimeToLive = channel.Read<uint>();
                referral2.DFSPathOffset = channel.Read<ushort>();
                referral2.DFSAlternatePathOffset = channel.Read<ushort>();
                referral2.NetworkAddressOffset = channel.Read<ushort>();

                if (i == 0 && referral2.DFSPathOffset == referral2.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
                    referral2.DFSPath = ReadDfsPath(channel);
                    ReadPadding(channel, referral2.DFSAlternatePathOffset - referral2.DFSPathOffset
                        - (referral2.DFSPath.Length + 1) * sizeofWord);

                    referral2.DFSAlternatePath = ReadDfsPath(channel);
                    ReadPadding(channel, referral2.NetworkAddressOffset - referral2.DFSAlternatePathOffset
                        - (referral2.DFSAlternatePath.Length + 1) * sizeofWord);

                    referral2.DFSTargetPath = ReadDfsPath(channel);
                }
                referral2List.Add(referral2);
            }

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

                for (int i = 0; i < referral2List.Count; i++)
                {
                    // Calculate the offsets of Dfs paths
                    int leftCount = referral2List.Count - i;
                    DFS_REFERRAL_V2 referral2 = referral2List[i];
                    int dfsPathOffset = referral2.DFSPathOffset - leftCount * referral2FixedSize;
                    int dfsAlternatePathOffset = referral2.DFSAlternatePathOffset -
                        leftCount * referral2FixedSize;
                    int targetPathOffset = referral2.NetworkAddressOffset - leftCount * referral2FixedSize;

                    // Get the Dfs paths
                    referral2.DFSPath = ReadDfsPath(pathData, dfsPathOffset);
                    referral2.DFSAlternatePath = ReadDfsPath(pathData, dfsAlternatePathOffset);
                    referral2.DFSTargetPath = ReadDfsPath(pathData, targetPathOffset);

                    referral2List[i] = referral2;
                }
            }
            this.referralResponse.ReferralEntries = referral2List.ToArray();
        }
        /// <summary>
        /// Decode the payload for type of Referral Entry 2
        /// </summary>
        /// <param name="channel">channel with payload data</param>
        /// <exception cref=" System.InvalidOperationException">
        /// Fail to decode payload data to ReferralV2 structure
        /// </exception>
        private void DecodeReferralV2(Channel channel)
        {
            bool isDfsPathFollowed = false;
            List <DFS_REFERRAL_V2> referral2List = new List <DFS_REFERRAL_V2>();

            for (int i = 0; i < this.referralResponse.NumberOfReferrals; i++)
            {
                DFS_REFERRAL_V2 referral2 = new DFS_REFERRAL_V2();
                // Read the fixed portion of DFS_REFERRAL_V2
                referral2.VersionNumber          = channel.Read <ushort>();
                referral2.Size                   = channel.Read <ushort>();
                referral2.ServerType             = channel.Read <ushort>();
                referral2.ReferralEntryFlags     = channel.Read <ushort>();
                referral2.Proximity              = channel.Read <uint>();
                referral2.TimeToLive             = channel.Read <uint>();
                referral2.DFSPathOffset          = channel.Read <ushort>();
                referral2.DFSAlternatePathOffset = channel.Read <ushort>();
                referral2.NetworkAddressOffset   = channel.Read <ushort>();

                if (i == 0 && referral2.DFSPathOffset == referral2.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
                    referral2.DFSPath = ReadDfsPath(channel);
                    ReadPadding(channel, referral2.DFSAlternatePathOffset - referral2.DFSPathOffset
                                - (referral2.DFSPath.Length + 1) * sizeofWord);

                    referral2.DFSAlternatePath = ReadDfsPath(channel);
                    ReadPadding(channel, referral2.NetworkAddressOffset - referral2.DFSAlternatePathOffset
                                - (referral2.DFSAlternatePath.Length + 1) * sizeofWord);

                    referral2.DFSTargetPath = ReadDfsPath(channel);
                }
                referral2List.Add(referral2);
            }

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

                for (int i = 0; i < referral2List.Count; i++)
                {
                    // Calculate the offsets of Dfs paths
                    int             leftCount              = referral2List.Count - i;
                    DFS_REFERRAL_V2 referral2              = referral2List[i];
                    int             dfsPathOffset          = referral2.DFSPathOffset - leftCount * referral2FixedSize;
                    int             dfsAlternatePathOffset = referral2.DFSAlternatePathOffset -
                                                             leftCount * referral2FixedSize;
                    int targetPathOffset = referral2.NetworkAddressOffset - leftCount * referral2FixedSize;

                    // Get the Dfs paths
                    referral2.DFSPath          = ReadDfsPath(pathData, dfsPathOffset);
                    referral2.DFSAlternatePath = ReadDfsPath(pathData, dfsAlternatePathOffset);
                    referral2.DFSTargetPath    = ReadDfsPath(pathData, targetPathOffset);

                    referral2List[i] = referral2;
                }
            }
            this.referralResponse.ReferralEntries = referral2List.ToArray();
        }