using System.Net; using System.Net.Sockets; using System.Threading; using System.Collections.Generic; namespace ConsoleChatServer { static class IPSorter { /// /// Find the first IPv6 address for a given host /// /// URL of the host to lookup /// IPAddress IPv6 or null if no IPv6 is associated public static IPAddress FindIPv6Address(string host) { IPAddress[] addressList = Dns.GetHostAddresses(host); foreach (IPAddress ip in addressList) { if (ip.AddressFamily == AddressFamily.InterNetworkV6) { return ip; } } return null; } /// /// Find the first IPv4 on for a given host /// /// URL of the host to lookup /// IPAddress IPv4 or null if no IPv4 address associated public static IPAddress FindIPv4Address(string host) { IPAddress[] addressList = Dns.GetHostAddresses(host); foreach (IPAddress ip in addressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { return ip; } } return null; } /// /// Attempts to find an IPv6 for the host, if none is found, returns IPv4 /// /// /// public static IPAddress GetBestAddress(string host) { IPAddress ip = FindIPv6Address(host); if (ip != null) { return ip; } return FindIPv4Address(host); } } }