public LocalOutgoingServerSession createOutgoingSession(int port) {
        final Logger log = LoggerFactory.getLogger( Log.getName() + "[Acting as Originating Server: Create Outgoing Session from: " + domainPair.getLocal() + " to a RS in the domain of: " + domainPair.getRemote() + " (port: " + port+ ")]" );

        log.debug( "Creating new outgoing session..." );

        if (!ServerDialback.isEnabled() && !ServerDialback.isEnabledForSelfSigned()) {
            log.info("Unable to create new outgoing session: Dialback has been disabled by configuration.");
            return null;
        }

        String hostname = null;
        int realPort = port;
        try {
            // Establish a TCP connection to the Receiving Server
            final Map.Entry<Socket, Boolean> socketToXmppDomain = SocketUtil.createSocketToXmppDomain(domainPair.getRemote(), port );
            if ( socketToXmppDomain  == null ) {
                log.info( "Unable to create new outgoing session: Cannot create a plain socket connection with any applicable remote host." );
                return null;
            }

            final Socket socket = socketToXmppDomain.getKey();
            final boolean directTLS = socketToXmppDomain.getValue();
            connection = new SocketConnection(XMPPServer.getInstance().getPacketDeliverer(), socket, false);
            if (directTLS) {
                connection.startTLS( false, directTLS );
            }

            log.debug( "Send the stream header and wait for response..." );
            final Element stream = DocumentHelper.createElement(QName.get("stream", "stream", "http://etherx.jabber.org/streams"));
            final Document document = DocumentHelper.createDocument(stream);
            document.setXMLEncoding(StandardCharsets.UTF_8.toString());
            stream.add(Namespace.get("", "jabber:server"));
            stream.add(Namespace.get("db", "jabber:server:dialback"));
            stream.addAttribute("to", domainPair.getRemote());
            stream.addAttribute("from", domainPair.getLocal());

            connection.deliverRawText(StringUtils.asUnclosedStream(document));

            // Set a read timeout (of 5 seconds) so we don't keep waiting forever
            int soTimeout = socket.getSoTimeout();
            socket.setSoTimeout(RemoteServerManager.getSocketTimeout());

            XMPPPacketReader reader = new XMPPPacketReader();
            reader.setXPPFactory(FACTORY);

            final InputStream input;
            if (directTLS) {
                input = ((SocketConnection)connection).getTLSStreamHandler().getInputStream();
            } else {
                input = socket.getInputStream();
            }
            reader.getXPPParser().setInput(new InputStreamReader(
                    ServerTrafficCounter.wrapInputStream(input), CHARSET));
            // Get the answer from the Receiving Server
            XmlPullParser xpp = reader.getXPPParser();
            for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;) {
                eventType = xpp.next();
            }
            log.debug( "Got a response. Check if the remote server supports dialback..." );

            if ("jabber:server:dialback".equals(xpp.getNamespace("db"))) {
                log.debug( "Dialback seems to be supported by the remote server." );

                // Restore default timeout
                socket.setSoTimeout(soTimeout);
                String id = xpp.getAttributeValue("", "id");
                OutgoingServerSocketReader socketReader = new OutgoingServerSocketReader(reader);
                if (authenticateDomain(socketReader, id)) {
                    log.debug( "Successfully authenticated the connection with dialback." );
                    // Domain was validated so create a new OutgoingServerSession
                    StreamID streamID = BasicStreamIDFactory.createStreamID(id);
                    LocalOutgoingServerSession session = new LocalOutgoingServerSession(domainPair.getLocal(), connection, socketReader, streamID);
                    connection.init(session);
                    session.setAuthenticationMethod(ServerSession.AuthenticationMethod.DIALBACK);
                    // Set the remote domain as the address of the session.
                    session.setAddress(new JID(null, domainPair.getRemote(), null));
                    log.debug( "Successfully created new outgoing session!" );
                    return session;
                }
                else {
                    log.debug( "Failed to authenticate the connection with dialback." );
                    // Close the connection
                    connection.close();
                }
            }
            else {
                log.debug("Error! Invalid namespace in packet: '{}'. Closing connection.", xpp.getText() );
                // Send an invalid-namespace stream error condition in the response
                connection.deliverRawText(
                        new StreamError(StreamError.Condition.invalid_namespace).toXML());
                // Close the connection
                connection.close();
            }
        }
        catch (Exception e) {
            log.error( "An exception occurred while creating outgoing session to remote server: ", e );
            // Close the connection
            if (connection != null) {
                connection.close();
            }
        }
        log.warn( "Unable to create a new outgoing session" );
        return null;
    }
Creates a new connection for the domain pair, where the local domain acts as the Originating Server and the
remote domain as the Receiving Server.

@param port port of the Receiving Server.
@return an OutgoingServerSession if the domain was authenticated or {@code null} if none.
xmppserver/src/main/java/org/jivesoftware/openfire/server/ServerDialback.java