AutomationDrivers.IisExpressHost.ProcessFactory.GetAvailablePort C# (CSharp) Method

GetAvailablePort() public static method

public static GetAvailablePort ( ) : int
return int
        public static int GetAvailablePort()
        {
            const int portStartIndex = 49152;
            const int portEndIndex = 65535;

            var properties = IPGlobalProperties.GetIPGlobalProperties();
            var tcpEndPoints = properties.GetActiveTcpListeners();

            var usedPorts = tcpEndPoints.Select(p => p.Port).ToList();
            var unusedPort = 0;

            for (var port = portStartIndex; port < portEndIndex; port++)
            {
                if (!usedPorts.Contains(port))
                {
                    unusedPort = port;
                    break;
                }
            }
            return unusedPort;
        }