Use ...bind(port = 0) and then Use serverSocket.localAddress.toJavaAddress().port
...bind(port = 0)
serverSocket.localAddress.toJavaAddress().port