using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; namespace ScreenConnect; public static class ClientNetworkExtensions { private static Dictionary ipAddressToMacAddressMap = new Dictionary(1); public static Socket ConnectTcpSocket(Uri endPointUri) { Exception ex = new InvalidOperationException("No IP address found for " + endPointUri.Host); IPAddress[] iPAddresses = Singleton.Instance.GetIPAddresses(endPointUri.Host); foreach (IPAddress iPAddress in iPAddresses) { Socket socket = new Socket(iPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { socket.Connect(iPAddress, endPointUri.Port); Extensions.Try(delegate { socket.NoDelay = true; Singleton.Instance.SetPlatformSocketOptions(socket, willBeAsync: false, Singleton.Instance.GetKeepAliveTimeSeconds() * 1000, 2000, 20000); }); return socket; } catch (Exception ex2) { socket.DisposeQuietly(); ex = ex2; } } throw ex; } public static byte[] GetMacAddressBytes(IPAddress localIPAddress) { lock (ipAddressToMacAddressMap) { return ipAddressToMacAddressMap.GetValueAndCreateIfNotFound(localIPAddress, () => (from it in NetworkInterface.GetAllNetworkInterfaces() where it.GetIPProperties().UnicastAddresses.Any((UnicastIPAddressInformation info) => object.Equals(info.Address, localIPAddress)) select it.GetPhysicalAddress().GetAddressBytes() into it where it.Length == 6 select it).FirstOrDefault()); } } }