private ServerSocket createServerSocket ( int backlog , InetAddress binAddr ) throws IOException { ServerSocket server = null ; int port = 0 ; while ( true ) { port = this . portRange . getFreePort ( port ) ; try { server = new PrServerSocket ( port , backlog , binAddr ) ; this . portRange . setUsed ( port ) ; return server ; } catch ( IOException e ) { // continue on port ++ ; } } }
Tries to find first available port within the port range specified . If it finds a free port it first checks if the port is not used by any other server . If it is it keeps looking for a next available port . If none found it throws an exception . If the port is available the server instance is returned .