protected String getBlockedInput(boolean echo, int timeout, boolean waitForCarriageReturn) {
        int x;
        boolean telnetNegotiation = false;

        try {
            do {
                try {
                    // Wait for timeout milliseconds, before attempting to read again.
                    socket.setSoTimeout(timeout);

                    // Get input from the socket.
                    x = in.read();

                    if (x == 10)
                        x = 0;

                    // If the input is -1 then the socket is probably bad.
                    if (x == -1)
                        return null;

                    // If 255, then it's a telnet negotiation command.
                    // Telnet negotiation capture.
                    if (x == 255) {
                        TrekLog.logMessage("Received Telnet IAC...");
                        telnetNegotiation = true;
                    }

                    if (x < 240 && x > 48)
                        telnetNegotiation = false;

                    if (telnetNegotiation) {
                        char[] response = telnet.negotiate(x);
                        if (response.length == 3) {
                            if (response[0] != '\0') {
                                byte[] responseBytes = new byte[3];

                                for (int y = 0; y < response.length; y++) {
                                    responseBytes[y] = (byte) response[y];
                                }

                                out.write(responseBytes);
                                out.flush();
                            }

                            telnetNegotiation = false;
                        }

                        x = 0;
                    }

                    if (!waitForCarriageReturn && x != 0)
                        return Character.toString((char) x);

                    if (bufferInput(x, echo))
                        break;

                } catch (SocketTimeoutException ste) {
                    state = WAIT_SOCKETTIMEDOUT;
                    TrekLog.logMessage("Socket timed out.  Disconnecting.");
                    return null;
                } catch (Exception e) {
                    TrekLog.logException(e);
                    return null;
                }

                if (x == -1) {
                    return null;
                }

                Thread.sleep(50);
            }
            while (in.available() != -1);
            return buffer;

        } catch (SocketException se) {
            TrekLog.logError(
                    shipName + " Socket Exception: " + se.getMessage());
            kill();
            return null;
        } catch (IOException ioe) {
            TrekLog.logError(shipName + " IOException: " + ioe.getMessage());
            kill();
            return null;
        } catch (Exception re) {
            TrekLog.logException(re);
            return null;
        }
    }