nextAvailablePort, give better error messages

Validate that the given argument is in the valid range for port numbers.
If not possible to find a port in the valid range, throw a descriptive
exception message (instead of trying an invalid port number and getting
an IllegalArgumentException from the JRE)
Fix illegal argument in isPortAvailable

JRE docs say that the wildcard address can only be used on bind()
operations, but there's a case where we might use it in a connect()
call - this is undefined behaviour. Correct the code to pass in the
loopback address to connect() instead of the wildcard address.