private List <ServiceInstanceEndPoint> LookupInstanceEndpoints(string name, ServiceInstanceFields fields)
        {
            List <ResourceRecord> records = DoLookup(name, RecordType.Service);

            List <ServiceInstanceEndPoint> endpoints = new List <ServiceInstanceEndPoint>();

            foreach (ServiceRecord record in records)
            {
                List <IPEndPoint> ipEndPoints = null;
                if ((fields & ServiceInstanceFields.IPAddresses) != 0)
                {
                    ipEndPoints = new List <IPEndPoint>();

                    List <ResourceRecord> ipRecords = DoLookup(record.Target, RecordType.Address);

                    foreach (IP4AddressRecord ipRecord in ipRecords)
                    {
                        ipEndPoints.Add(new IPEndPoint(ipRecord.Address, record.Port));
                    }

                    List <ResourceRecord> ip6Records = DoLookup(record.Target, RecordType.IP6Address);

                    // foreach (Ip6AddressRecord ipRecord in ipRecords)
                    // {
                    //     ipEndPoints.Add(new IPEndPoint(ipRecord.Address, record.Port));
                    // }
                }

                endpoints.Add(new ServiceInstanceEndPoint(record.Priority, record.Weight, record.Port, record.Target, ipEndPoints.ToArray()));
            }

            return(endpoints);
        }
        /// <summary>
        /// Queries for all instances of a particular service on the local network.
        /// </summary>
        /// <param name="service">The service to query, for example "_http._tcp".</param>
        /// <param name="domain">The domain to query.</param>
        /// <param name="fields">The details to query.</param>
        /// <returns>An array of service instances.</returns>
        /// <remarks>Excluding some fields (for example the IP address) may reduce the time taken.</remarks>
        public ServiceInstance[] LookupInstances(string service, string domain, ServiceInstanceFields fields)
        {
            List <ResourceRecord> records = DoLookup(service + "." + domain, RecordType.Pointer);

            List <ServiceInstance> instances = new List <ServiceInstance>();

            foreach (PointerRecord record in records)
            {
                instances.Add(LookupInstance(EncodeName(record.TargetName, record.Name), fields));
            }

            return(instances.ToArray());
        }
        /// <summary>
        /// Queries for all instances of a particular service on the local network.
        /// </summary>
        /// <param name="name">The instance to query, for example "My WebServer._http._tcp".</param>
        /// <param name="fields">The details to query.</param>
        /// <returns>The service instance.</returns>
        /// <remarks>Excluding some fields (for example the IP address) may reduce the time taken.</remarks>
        public ServiceInstance LookupInstance(string name, ServiceInstanceFields fields)
        {
            ServiceInstance instance = new ServiceInstance(name);

            if ((fields & ServiceInstanceFields.DisplayName) != 0)
            {
                instance.DisplayName = DecodeDisplayName(name);
            }

            if ((fields & ServiceInstanceFields.Parameters) != 0)
            {
                instance.Parameters = LookupInstanceDetails(name);
            }

            if ((fields & ServiceInstanceFields.DnsAddresses) != 0 ||
                (fields & ServiceInstanceFields.IPAddresses) != 0)
            {
                instance.EndPoints = LookupInstanceEndpoints(name, fields);
            }

            return(instance);
        }
        /// <summary>
        /// Queries for all instances of a particular service on the local network.
        /// </summary>
        /// <param name="name">The instance to query, for example "My WebServer._http._tcp"</param>
        /// <param name="fields">The details to query</param>
        /// <returns>The service instance</returns>
        /// <remarks>Excluding some fields (for example the IP address) may reduce the time taken</remarks>
        public ServiceInstance LookupInstance(string name, ServiceInstanceFields fields)
        {
            ServiceInstance instance = new ServiceInstance(name);

            if ((fields & ServiceInstanceFields.DisplayName) != 0)
            {
                instance.DisplayName = DecodeDisplayName(name);
            }

            if ((fields & ServiceInstanceFields.Parameters) != 0)
            {
                instance.Parameters = LookupInstanceDetails(name);
            }

            if ((fields & ServiceInstanceFields.DnsAddresses) != 0
                || (fields & ServiceInstanceFields.IPAddresses) != 0)
            {
                instance.EndPoints = LookupInstanceEndpoints(name, fields);
            }

            return instance;
        }
        private List<ServiceInstanceEndPoint> LookupInstanceEndpoints(string name, ServiceInstanceFields fields)
        {
            List<ResourceRecord> records = DoLookup(name, RecordType.Service);

            List<ServiceInstanceEndPoint> endpoints = new List<ServiceInstanceEndPoint>();

            foreach (ServiceRecord record in records)
            {
                List<IPEndPoint> ipEndPoints = null;
                if ((fields & ServiceInstanceFields.IPAddresses) != 0)
                {
                    ipEndPoints = new List<IPEndPoint>();

                    List<ResourceRecord> ipRecords = DoLookup(record.Target, RecordType.Address);

                    foreach (IP4AddressRecord ipRecord in ipRecords)
                    {
                        ipEndPoints.Add(new IPEndPoint(ipRecord.Address, record.Port));
                    }

                    List<ResourceRecord> ip6Records = DoLookup(record.Target, RecordType.IP6Address);

                    // foreach (Ip6AddressRecord ipRecord in ipRecords)
                    // {
                    //     ipEndPoints.Add(new IPEndPoint(ipRecord.Address, record.Port));
                    // }
                }

                endpoints.Add(new ServiceInstanceEndPoint(record.Priority, record.Weight, record.Port, record.Target, ipEndPoints.ToArray()));
            }

            return endpoints;
        }
        /// <summary>
        /// Queries for all instances of a particular service on the local network.
        /// </summary>
        /// <param name="service">The service to query, for example "_http._tcp".</param>
        /// <param name="domain">The domain to query.</param>
        /// <param name="fields">The details to query.</param>
        /// <returns>An array of service instances.</returns>
        /// <remarks>Excluding some fields (for example the IP address) may reduce the time taken.</remarks>
        public ServiceInstance[] LookupInstances(string service, string domain, ServiceInstanceFields fields)
        {
            List<ResourceRecord> records = DoLookup(service + "." + domain, RecordType.Pointer);

            List<ServiceInstance> instances = new List<ServiceInstance>();
            foreach (PointerRecord record in records)
            {
                instances.Add(LookupInstance(EncodeName(record.TargetName, record.Name), fields));
            }

            return instances.ToArray();
        }
 /// <summary>
 /// Queries for all instances of a particular service on the local network.
 /// </summary>
 /// <param name="service">The service to query, for example "_http._tcp".</param>
 /// <param name="fields">The details to query.</param>
 /// <returns>An array of service instances.</returns>
 /// <remarks>Excluding some fields (for example the IP address) may reduce the time taken.</remarks>
 public ServiceInstance[] LookupInstances(string service, ServiceInstanceFields fields)
 {
     return LookupInstances(service, "local.", fields);
 }
 /// <summary>
 /// Queries for all instances of a particular service on the local network.
 /// </summary>
 /// <param name="service">The service to query, for example "_http._tcp".</param>
 /// <param name="fields">The details to query.</param>
 /// <returns>An array of service instances.</returns>
 /// <remarks>Excluding some fields (for example the IP address) may reduce the time taken.</remarks>
 public ServiceInstance[] LookupInstances(string service, ServiceInstanceFields fields)
 {
     return(LookupInstances(service, "local.", fields));
 }