public static int findFreePort() { int portUsed; do { portUsed = PORT.getAndIncrement(); } while (!available(portUsed)); return portUsed; } private static boolean available(int port) { try (Socket ignored = new Socket("localhost", port)) { return false; } catch (IOException ignored) { return true; } }