code: public static ServerSocket createSocketFromPorts(Iterator<Integer> portsIterator, SocketFactory factory) {
  		while (portsIterator.hasNext()) {
  			int port = portsIterator.next();
  			LOG.debug("Trying to open socket on port {}", port);
  			try {
  				return factory.createSocket(port);
  			} catch (IOException | IllegalArgumentException e) {
  				if (LOG.isDebugEnabled()) {
  					LOG.debug("Unable to allocate socket on port", e);
  				} else {
  					LOG.info("Unable to allocate on port {}, due to error: {}", port, e.getMessage());
  				}
  			}
  		}
  		return null;
  	}
docs: Tries to allocate a socket from the given sets of ports.
  
  @param portsIterator A set of ports to choose from.
  @param factory A factory for creating the SocketServer
  @return null if no port was available or an allocated socket.