Source:
<code>public CompletableFuture<LoginMessage> connectAndLogIn(String username, String password) { sessionFuture = new CompletableFuture<>(); loginFuture = new CompletableFuture<>(); this.username = username; this.password = password; fafConnectionTask = new Task<Void>() { @Override protected Void call() throws Exception { while (!isCancelled()) { Server server = clientProperties.getServer(); String serverHost = server.getHost(); int serverPort = server.getPort(); logger.info("Trying to connect to FAF server at {}:{}", serverHost, serverPort); Platform.runLater(() -> connectionState.set(ConnectionState.CONNECTING)); try (Socket fafServerSocket = new Socket(serverHost, serverPort); OutputStream outputStream = fafServerSocket.getOutputStream()) { FafServerAccessorImpl.this.fafServerSocket = fafServerSocket; fafServerSocket.setKeepAlive(true); localIp = fafServerSocket.getLocalAddress().getHostAddress(); serverWriter = createServerWriter(outputStream); writeToServer(new InitSessionMessage(Version.getCurrentVersion())); logger.info("FAF server connection established"); Platform.runLater(() -> connectionState.set(ConnectionState.CONNECTED)); <START> reconnectService.resetConnectionFailures(); <END> blockingReadServer(fafServerSocket); } catch (IOException e) { Platform.runLater(() -> connectionState.set(ConnectionState.DISCONNECTED)); if (isCancelled()) { logger.debug("Connection to FAF server has been closed"); } else { logger.warn("Lost connection to Server", e); reconnectService.incrementConnectionFailures(); reconnectService.waitForReconnect(); } } } return null; } @Override protected void cancelled() { IOUtils.closeQuietly(serverWriter); IOUtils.closeQuietly(fafServerSocket); logger.debug("Closed connection to FAF lobby server"); } }; executeInBackground(fafConnectionTask); return loginFuture; }</code><technical_language>This belongs in reconnect service (and triggered events message bus?)</technical_language>

Target:
public CompletableFuture<LoginMessage> connectAndLogIn(String username, String password) { sessionFuture = new CompletableFuture<>(); loginFuture = new CompletableFuture<>(); this.username = username; this.password = password; fafConnectionTask = new Task<Void>() { @Override protected Void call() throws Exception { while (!isCancelled()) { Server server = clientProperties.getServer(); String serverHost = server.getHost(); int serverPort = server.getPort(); log.info("Trying to connect to FAF server at {}:{}", serverHost, serverPort); Platform.runLater(() -> connectionState.set(ConnectionState.CONNECTING)); try (Socket fafServerSocket = new Socket(serverHost, serverPort); OutputStream outputStream = fafServerSocket.getOutputStream()) { FafServerAccessorImpl.this.fafServerSocket = fafServerSocket; fafServerSocket.setKeepAlive(true); localIp = fafServerSocket.getLocalAddress().getHostAddress(); serverWriter = createServerWriter(outputStream); writeToServer(new InitSessionMessage(Version.getCurrentVersion())); log.info("FAF server connection established"); Platform.runLater(() -> connectionState.set(ConnectionState.CONNECTED)); reconnectTimerService.resetConnectionFailures(); blockingReadServer(fafServerSocket); } catch (IOException e) { Platform.runLater(() -> connectionState.set(ConnectionState.DISCONNECTED)); if (isCancelled()) { log.debug("Connection to FAF server has been closed"); } else { log.warn("Lost connection to Server", e); reconnectTimerService.incrementConnectionFailures(); reconnectTimerService.waitForReconnect(); } } } return null; } @Override protected void cancelled() { IOUtils.closeQuietly(serverWriter); IOUtils.closeQuietly(fafServerSocket); log.debug("Closed connection to FAF lobby server"); } }; executeInBackground(fafConnectionTask); return loginFuture; }