private static ZeroconfRecord DnsToZeroconf(DnsMessage message)
 {
     if (!message.IsResponse)
         return null;
     var zr = new ZeroconfRecord();
     var ptr = message.Records.OfType<PtrRecord>().FirstOrDefault();
     if (ptr != null)
         zr.Name = ptr.DomainNamePointer.Split('.')[0];
     var hst = message.Records.OfType<HostAddressRecord>().FirstOrDefault();
     if (hst != null)
     {
         zr.Host = hst.ResourceName.Split('.')[0];
         zr.IPAddress = hst.IPAddress;
     }
     var srv = message.Records.OfType<SrvRecord>().FirstOrDefault();
     if (srv != null)
         zr.Port = srv.Port.ToString();
     return zr;
 }
        private static DnsMessage ReadDnsMessage(IDataReader dataReader)
        {
            var stringReader = new DnsStringReader(dataReader, dataReader.UnconsumedBufferLength);
            var msg = new DnsMessage
                {
                    QueryIdentifier = dataReader.ReadUInt16(),
                    Flags = dataReader.ReadUInt16(),
                };

            var qCount = dataReader.ReadUInt16();
            var aCount = dataReader.ReadUInt16();
            var nsCount = dataReader.ReadUInt16();
            var arCount = dataReader.ReadUInt16();
            var nonQuestions = aCount + nsCount + arCount;

            for (var i = 0; i < qCount; i++)
                msg.Records.Add(new QuestionRecord
                              {
                                  ResourceName = stringReader.ReadString(),
                                  QuestionType = dataReader.ReadUInt16(),
                                  Class = dataReader.ReadUInt16()
                              });

            for (var i = 0; i < nonQuestions; i++)
            {
                var resName = stringReader.ReadString();
                var resType = dataReader.ReadUInt16();
                var resClass = dataReader.ReadUInt16();
                var ttl = dataReader.ReadUInt32();
                var resLength = dataReader.ReadUInt16();
                var remaining = dataReader.UnconsumedBufferLength;
                ResourceRecord rec;
                switch (resType)
                {
                    case 1:  // A,
                        rec = new HostAddressRecord
                        {
                            IPAddress = string.Format("{0}.{1}.{2}.{3}",
                                                                dataReader.ReadByte(), dataReader.ReadByte(),
                                                                dataReader.ReadByte(), dataReader.ReadByte())
                        };
                        break;
                    case 12:  // PTR
                        rec = new PtrRecord
                        {
                            DomainNamePointer = stringReader.ReadString(resLength)
                        };
                        break;
                    case 16:  // TXT
                        rec = new TxtRecord
                        {
                            TextRData = stringReader.ReadString(resLength)
                        };
                        break;
                    case 33:  // SRV
                        rec = new SrvRecord
                        {
                            Priority = dataReader.ReadUInt16(),
                            Weight = dataReader.ReadUInt16(),
                            Port = dataReader.ReadUInt16(),
                            Target = stringReader.ReadString(resLength)
                        };
                        break;
                    default:
                        rec = new UnknownDnsRecord
                        {
                            ResourceType = resType
                        };
                        break;
                }
                rec.ResourceName = resName;
                rec.Class = resClass;
                rec.Ttl = ttl;
                msg.Records.Add(rec);
                var remainingResourceBytes = (int)(resLength - (remaining - dataReader.UnconsumedBufferLength));
                if (remainingResourceBytes < 0)
                {
                    Debug.WriteLine("error reading resource - reached into next record");
                    return msg;
                }
                if (remainingResourceBytes > 0)
                    dataReader.ReadBuffer((uint)remainingResourceBytes);
            }

            return msg;
        }