@Nullable
    private InputStream connect(int port) throws IOException {
        final long s = System.currentTimeMillis();
        final InetSocketAddress endpoint = new InetSocketAddress(InetAddress.getLoopbackAddress(), port);

        myStartedProcess.notifyTextAvailable("Connecting to XSLT runner on " + endpoint + "\n", ProcessOutputTypes.SYSTEM);

        int tries = 0;
        IOException ex;
        do {
            final int d = (int)(System.currentTimeMillis() - s);
            try {
                @SuppressWarnings({"SocketOpenedButNotSafelyClosed"})
                final Socket socket = new Socket();
                socket.connect(endpoint, Math.max(CONNECT_TIMEOUT - d, 100));

                myStartedProcess.notifyTextAvailable("Connected to XSLT runner." + "\n", ProcessOutputTypes.SYSTEM);
                return socket.getInputStream();
            } catch (ConnectException e) {
                ex = e;
                try { Thread.sleep(500); } catch (InterruptedException ignored) { break; }
            }
            if (myStartedProcess.isProcessTerminated() || myStartedProcess.isProcessTerminating()) {
                return null;
            }
        } while (tries++ < 10);

        throw ex;
    }
@Nullable
    private InputStream connect(int port)