void () { setStatus(ConnectionStatus.WAITING_FOR_CONNECTION, null); ApplicationManager.getApplication().executeOnPooledThread(() -> { Exception exception = null; InetAddress host = myHost; if (host == null) { host = InetAddress.getLoopbackAddress(); } for (int attempt = 0; attempt < MAX_CONNECTION_ATTEMPTS; attempt++) { for (int i = 0; i < myPortsNumberToTry; i++) { Socket socket; try { //noinspection SocketOpenedButNotSafelyClosed socket = new Socket(host, myInitialPort + i); } catch (IOException e) { LOG.debug(e); exception = e; continue; } setPort(socket.getPort()); try { attachToSocket(socket); } catch (IOException e) { LOG.info(e); } return; } try { //noinspection BusyWait Thread.sleep(CONNECTION_ATTEMPT_DELAY); } catch (InterruptedException e) { exception = e; break; } } setStatus(ConnectionStatus.CONNECTION_FAILED, exception == null ? "Connection failed" : "Connection failed: " + exception.getMessage()); }); }