```Java
for (int i = 40000; i < 40100; i++) {
    try {
        ServerSocket s = new ServerSocket(i);
        assertNotNull(s, "Server socket was not created for port {}", i);
        return s;
    } catch (IOException e) {
        // try next number
    }
}
throw new IllegalStateException("No ports available in range!");
```