Gwupe.Communication.P2P.P2P.Tunnel.Waver.GetDefaultIp C# (CSharp) Method

GetDefaultIp() private method

private GetDefaultIp ( IPEndPoint ip ) : IPAddress
ip System.Net.IPEndPoint
return System.Net.IPAddress
        private IPAddress GetDefaultIp(IPEndPoint ip)
        {
            var ping = new Ping();
            var options = new PingOptions { Ttl = 1 };
            PingReply reply = ping.Send(ip.Address.ToString(), 5000, new byte[32], options);
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface nic in adapters)
            {
                foreach (GatewayIPAddressInformation gw in nic.GetIPProperties().GatewayAddresses)
                {
                    if (gw.Address.Equals(reply.Address))
                    {
                        foreach (UnicastIPAddressInformation ipInfo in nic.GetIPProperties().UnicastAddresses)
                        {
                            if (!ipInfo.Address.IsIPv6LinkLocal)
                            {
                                return ipInfo.Address;
                            }
                        }
                    }
                }
            }
            return IPAddress.Any;
        }