@Override
        public void run()
        {
            log("Start reading from socket.");
            try {
                final ByteBuffer buffer = ByteBuffer.allocate(1024*16);
                ReadableByteChannel channel = Channels.newChannel(is);
                do {
                    try {
                        while (!processingService.isShutdown() && channel.read(buffer) >= 0) {
                            buffer.flip();
                            String read = StandardCharsets.UTF_8.decode(buffer).toString();
                            buffer.compact();
                            // Ugly hack to get Dialback to work.
                            if (read.startsWith("<db:") && !read.contains("xmlns:db=")) {
                                read = read.replaceFirst(" ", " xmlns:db=\"jabber:server:dialback\" ");
                                log("# recv (Hacked inbound stanza to include Dialback namespace declaration)" + (socket instanceof SSLSocket ? " (encrypted)" : ""));
                            } else {
                                log("# recv from Openfire" + (socket instanceof SSLSocket ? " (encrypted)" : ""));
                            }
                            log(read);
                            log("");

                            // THIS CONTROLS THE REMOTE SERVER TLS / AUTH RESPONSES
                            if (read.startsWith("<stream:error ") && read.endsWith("</stream:stream>")) {
                                log("Peer sends a stream error. Can't use this connection anymore.");
                                return;
                            }
                            if (!read.equals("</stream:stream>")) {
                                final Element inbound = parse(read);
                                switch (inbound.getName()) {
                                    case "stream":
                                        sendStreamHeader(inbound);
                                        sendStreamFeatures();
                                        break;
                                    case "starttls":
                                        sendStartTlsProceed(inbound);
                                        return; // Stop reading from this socket immediately, as it is replaced by a secure socket.
                                    case "auth":
                                        sendAuthResponse(inbound);
                                        break;
                                    case "result":
                                        processDialback(inbound);
                                        break;
                                    default:
                                        log("Received stanza '" + inbound.getName() + "' that I don't know how to respond to.");
                                }
                            }
                        }
                    } catch (SocketTimeoutException e) {
                        allowableSocketTimeouts--;
                        if (allowableSocketTimeouts <= 0) {
                            throw e;
                        }
                    }
                } while (!processingService.isShutdown() && allowableSocketTimeouts > 0);
                log("Ending read loop." + (socket instanceof SSLSocket ? " (encrypted)" : ""));
            } catch (Throwable t) {
                // Log exception only when not cleanly closed.
                if (doLog && !processingService.isShutdown()) {
                    t.printStackTrace();
                }
            } finally {
                log("Stopped reading from socket");
            }
        }
To speed up the test execution, SO_TIMEOUT (the socket read timeout) has been set to a low value. When negotiating Server Dialback, a second
socket connection is used. XMPP session establishment on the first socket connection is paused while the Server Dialback negotiation takes
place. This can easily cause the SO_TIMEOUT to run out. To prevent issues, this implementation allows for reads of the first socket connection
to time out for a certain number of times, before treating this as a terminal exception.
xmppserver/src/test/java/org/jivesoftware/openfire/session/RemoteReceivingServerDummy.java