Checks if the given port at the given host is available. Note: Socket will be closed after
the test.

@param host
the host name.
@param port
the port number.
@return If the given port at the given host is available true, otherwise false.
321019
public static boolean available(final String host, final int port)
	{
		Socket socket = null;
		try
		{
			socket = newSocket(host, port);
			return false;
		}
		catch (final IOException ignored)
		{
			return true;
		}
		finally
		{
			if (!closeClientSocket(socket))
			{
				LOGGER.error("Socket could not be closed on host " + host + " on port " + port);
			}
		}
	}
781510
function checkPort(port, host, callback) {
    var socket = new Socket(),
        status = null;
    socket.on('connect', () => {
        status = 'open';
        socket.end();
    });
    socket.setTimeout(1500);
    socket.on('timeout', () => {
        status = 'closed';
        socket.destroy();
    });
    socket.on('error', (exception) => {
        status = 'closed';
    });
    socket.on('close', (exception) => {
        callback(null, status,host,port);
    });
    socket.connect(port, host);
}
724807
protected Socket openSocket(String host, int port)
	throws IOException {
        return SocketFactory.getDefault().createSocket(host, port);
    }
1186034
@Trivial
    public static boolean validLocalHostName(String hostName) {
        InetAddress addr = findLocalHostAddress(hostName, PREFER_IPV6);
        return addr != null;
    }
265369
protected function check_port($port = 8000, $host = '127.0.0.1', $timeout = 3)
    {
        $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
        if (!$fp) {
            return true;
        } else {
            fclose($fp);

            return false;
        }
    }
715443
public boolean isLocalHostname(String host) {
		checkState();
		try {
			InetAddress[] addresses = InetAddress.getAllByName(host);

			Set<InetAddress> stackAddresses = new HashSet<InetAddress>();

			for (ListeningPoint lp : this.provider.getListeningPoints()) {
				InetAddress tmp = InetAddress.getByName(lp.getIPAddress());
				if (tmp != null)
					stackAddresses.add(tmp);
			}

			for (InetAddress ia : addresses) {
				if (stackAddresses.contains(ia))
					return true;
			}

		} catch (UnknownHostException e) {
			if (tracer.isFineEnabled()) {
				tracer.fine("Unknown host",e);
			}
		}

		return false;
	}
1344235
private boolean isHeaderExternal(String host, int port,String transport, boolean isIpv6) 
    {  

    	if(!isIpv6)
    	{
    		if (host.equalsIgnoreCase(balancerRunner.balancerContext.externalHost))
    		{
    			if(balancerRunner.balancerContext.getExternalPortByTransport(transport,false)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getExternalLoadBalancerPortByTransport(transport,false)==port)
    				return false;
    		}

    		if(host.equalsIgnoreCase(balancerRunner.balancerContext.internalHost))
    		{
    			if(balancerRunner.balancerContext.getInternalPortByTransport(transport,false)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getInternalLoadBalancerPortByTransport(transport,false)==port)
    				return false;
    		}
		
    		if(balancerRunner.balancerContext.externalIpLoadBalancerAddresses!=null&&
    				balancerRunner.balancerContext.externalIpLoadBalancerAddresses.contains(host))
    		{
    			if(balancerRunner.balancerContext.getExternalPortByTransport(transport,false)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getExternalLoadBalancerPortByTransport(transport,false)==port)
    				return false;
    		}
		
    		if(balancerRunner.balancerContext.internalIpLoadBalancerAddresses!=null&&
    				balancerRunner.balancerContext.internalIpLoadBalancerAddresses.contains(host))
    		{
    			if(balancerRunner.balancerContext.getInternalPortByTransport(transport,false)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getInternalLoadBalancerPortByTransport(transport,false)==port)
    				return false;
    		}
		
    		if(host.equalsIgnoreCase(balancerRunner.balancerContext.publicIP))
    		{
    			if(balancerRunner.balancerContext.getExternalPortByTransport(transport,false)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getExternalLoadBalancerPortByTransport(transport,false)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getInternalPortByTransport(transport,false)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getInternalLoadBalancerPortByTransport(transport,false)==port)
    				return false;
    		}

    		return true;
    	}
    	else
    	{
    		String cleanHost=host;
    		if(cleanHost.startsWith("["))
    			cleanHost=cleanHost.substring(1);
    		
    		if(cleanHost.endsWith("]"))
    			cleanHost=cleanHost.substring(0,cleanHost.length()-1);
    		
    		InetAddress address=null;
    		try
    		{
    			address=InetAddress.getByName(cleanHost);
    		}
    		catch(UnknownHostException ex)
    		{
    			
    		}
    		
    		if(address==null)
    			return false;
    		
    		if (address.equals(balancerRunner.balancerContext.externalIpv6HostAddress))
    		{    			
    			if(balancerRunner.balancerContext.getExternalPortByTransport(transport,true)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getExternalLoadBalancerPortByTransport(transport,true)==port)
    				return false;
    		}
    			

    		if(address.equals(balancerRunner.balancerContext.internalIpv6HostAddress))
    		{
    			if(balancerRunner.balancerContext.getInternalPortByTransport(transport,true)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getInternalLoadBalancerPortByTransport(transport,true)==port)
    				return false;
    		}
    		
    		if(balancerRunner.balancerContext.externalIpv6LoadBalancerAddressHosts.contains(address))
    		{
    			if(balancerRunner.balancerContext.getExternalPortByTransport(transport,true)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getExternalLoadBalancerPortByTransport(transport,true)==port)
    				return false;
    		}
    		
    		if(balancerRunner.balancerContext.internalIpv6LoadBalancerAddressHosts.contains(address))
    		{
    			if(balancerRunner.balancerContext.getInternalPortByTransport(transport,true)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getInternalLoadBalancerPortByTransport(transport,true)==port)
    				return false;
    		}
    		
    		if(address.equals(balancerRunner.balancerContext.publicIPv6Host))
    		{
    			if(balancerRunner.balancerContext.getExternalPortByTransport(transport,true)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getExternalLoadBalancerPortByTransport(transport,true)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getInternalPortByTransport(transport,true)==port)
    				return false;
			
    			if(balancerRunner.balancerContext.getInternalLoadBalancerPortByTransport(transport,true)==port)
    				return false;
    		}

		return true;
    	}
    }
457081
boolean isFree( int port )
    {
        try
        {
            ServerSocket sock = new ServerSocket( port );
            sock.close();
            // is free:
            return true;
            // We rely on an exception thrown to determine availability or not availability.
        } catch( Exception e )
        {
            // not free.
            return false;
        }
    }
281461
public void setHost(String host) throws IllegalStateException {
        if (host == null) {
            throw new IllegalArgumentException("host parameter is null");
        }
        assertNotOpen();
        hostName = host;
    }
369677
def port_is_used(port, host='127.0.0.1'):
    
    unused = _can_bind(port, host) and _refuses_connection(port, host)
    return not unused
1057655
public static boolean available(int port) {
        if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
            throw new IllegalArgumentException("Invalid start port: " + port);
        }

        ServerSocket ss = null;
        DatagramSocket ds = null;
        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);
            return true;
        } catch (IOException e) {
            // Do nothing
        } finally {
            if (ds != null) {
                ds.close();
            }

            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    /* should not be thrown */
                }
            }
        }

        return false;
    }
281378
public final void setHost(final String host) throws UnknownHostException {
        this.host = host;
        final InetAddress[] inetAddresses = InetAddress.getAllByName(host);

        for (InetAddress address: inetAddresses) {
            final AddressHostMatcher matcher = new AddressHostMatcher();
            matcher.setIp(address.getHostAddress());
            this.matchersForHost.add(matcher);
        }
    }
495574
function checkAvailable(host, port) {
  return new Promise(function(resolve, reject) {
    const socket = new net.Socket();

    socket.on('connect', () => {
      cleanupSocket(socket);
      resolve(true);
    });

    socket.on('error', err => {
      cleanupSocket(socket);
      if (err.code !== 'ECONNREFUSED') {
        return reject(err);
      }

      resolve(false);
    });

    socket.connect({ port: port, host: host });
  });
}
1301693
public static InetSocketAddress getAddress (String hostname, int port)
    {
        return StringUtil.isBlank(hostname) ?
            new InetSocketAddress(port) : new InetSocketAddress(hostname, port);
    }
448751
protected void connect(String host, int port)
        throws UnknownHostException, IOException
    {
        boolean connected = false;
        try {
            InetAddress address = InetAddress.getByName(host);
            this.port = port;
            this.address = address;

            connectToAddress(address, port, timeout);
            connected = true;
        } finally {
            if (!connected) {
                try {
                    close();
                } catch (IOException ioe) {
                    /* Do nothing. If connect threw an exception then
                       it will be passed up the call stack */
                }
            }
        }
    }
182032
public static ServerConnector createSocketConnector(Server server, String host, int port) {
        Assert.notNull(server, "'server' must not be null");
        Assert.notNull(host, "'host' must not be null");

        HttpConnectionFactory httpConnectionFactory = createHttpConnectionFactory();
        ServerConnector connector = new ServerConnector(server, httpConnectionFactory);
        initializeConnector(connector, host, port);
        return connector;
    }
215190
public static int getAvailablePort(String host, int port, int maxPort) {
        if (isAnyHost(host)
            || isLocalHost(host)
            || isHostInNetworkCard(host)) {
            if (port < MIN_PORT) {
                port = MIN_PORT;
            }
            for (int i = port; i <= maxPort; i++) {
                ServerSocket ss = null;
                try {
                    ss = new ServerSocket();
                    ss.bind(new InetSocketAddress(host, i));
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("ip:{} port:{} is available", host, i);
                    }
                    return i;
                } catch (IOException e) {
                    // continue
                    if (LOGGER.isWarnEnabled()) {
                        LOGGER.warn("Can't bind to address [{}:{}], " +
                            "Maybe 1) The port has been bound. " +
                            "2) The network card of this host is not exists or disable. " +
                            "3) The host is wrong.", host, i);
                    }
                    if (LOGGER.isInfoEnabled()) {
                        LOGGER.info("Begin try next port(auto +1):{}", i + 1);
                    }
                } finally {
                    IOUtils.closeQuietly(ss);
                }
            }
            throw new SofaRpcRuntimeException("Can't bind to ANY port of " + host + ", please check config");
        } else {
            throw new SofaRpcRuntimeException("The host " + host
                + " is not found in network cards, please check config");
        }
    }