/// <summary> Constructs the response object based upon the data received from the DNS server. </summary>
        /// <param name="responseData">The data (byte[]) from the DNS server.</param>
        /// <returns>Response message object.</returns>
        public DnsResponseMessage ProcessResponse(ArraySegment <byte> responseData)
        {
            IDnsRecordFactory factory = new DnsRecordFactory(reader);

            /*
             * From index 0 till 11 bytes in the responseData array, we have the header items.
             */

            ushort id              = reader.ReadUInt16NetworkOrder(responseData);
            ushort flags           = reader.ReadUInt16NetworkOrder(responseData);
            ushort questionCount   = reader.ReadUInt16NetworkOrder(responseData);
            ushort answerCount     = reader.ReadUInt16NetworkOrder(responseData);
            ushort nameServerCount = reader.ReadUInt16NetworkOrder(responseData);
            ushort additionalCount = reader.ReadUInt16NetworkOrder(responseData);

            /*
             * We have got above data, now build the response header.
             */
            DnsResponseHeader  header   = new DnsResponseHeader(id, flags, questionCount, answerCount, additionalCount, nameServerCount);
            DnsResponseMessage response = new DnsResponseMessage(header, responseData.Count);

            /*
             * Next item in the response data is question that we sent to the DNS server which the server has copied onto the response message.
             */
            for (int questionIndex = 0; questionIndex < questionCount; questionIndex++)
            {
                IDnsString  questionString = reader.ReadQuestionQueryString(responseData);
                DnsQuestion question       = new DnsQuestion(questionString, (QueryType)reader.ReadUInt16NetworkOrder(responseData), (QueryClass)reader.ReadUInt16NetworkOrder(responseData));
                response.AddQuestion(question);
            }

            for (int answerIndex = 0; answerIndex < answerCount; answerIndex++)
            {
                BaseResourceRecordInfo info   = factory.ReadRecordInfo(responseData);
                DnsResourceRecord      record = factory.GetRecord(info, responseData);
                response.AddAnswer(record);
            }

            for (int serverIndex = 0; serverIndex < nameServerCount; serverIndex++)
            {
                BaseResourceRecordInfo info   = factory.ReadRecordInfo(responseData);
                DnsResourceRecord      record = factory.GetRecord(info, responseData);
                response.AddAuthority(record);
            }

            for (int additionalIndex = 0; additionalIndex < additionalCount; additionalIndex++)
            {
                BaseResourceRecordInfo info   = factory.ReadRecordInfo(responseData);
                DnsResourceRecord      record = factory.GetRecord(info, responseData);
                response.AddAdditional(record);
            }

            return(response);
        }
            public IDnsQueryResponse GetResponse()
            {
                var elapsed = Elapsed();

                if (elapsed <= 0)
                {
                    return(_response);
                }

                var response = new DnsResponseMessage(
                    _response.Header,
                    _response.MessageSize)
                {
                    Audit = (_response as DnsQueryResponse)?.Audit ?? new LookupClientAudit()
                };

                foreach (var record in _response.Questions)
                {
                    response.AddQuestion(record);
                }

                foreach (var record in _response.Answers)
                {
                    var clone = record.Clone();
                    clone.TimeToLive = clone.TimeToLive - elapsed;
                    response.AddAnswer(clone);
                }

                foreach (var record in _response.Additionals)
                {
                    var clone = record.Clone();
                    clone.TimeToLive = clone.TimeToLive - elapsed;
                    response.AddAnswer(clone);
                }

                foreach (var record in _response.Authorities)
                {
                    var clone = record.Clone();
                    clone.TimeToLive = clone.TimeToLive - elapsed;
                    response.AddAnswer(clone);
                }

                var qr = response.AsQueryResponse(_response.NameServer);

                return(qr);
            }