+ return true;
+end
+
+function make_connect(host_session, connect_host, connect_port)
+ host_session.log("info", "Beginning new connection attempt to %s (%s:%d)", host_session.to_host, connect_host, connect_port);
+ -- Ok, we're going to try to connect
+
+ local from_host, to_host = host_session.from_host, host_session.to_host;
+
+ local conn, handler = socket.tcp()
+
+ conn:settimeout(0);
+ local success, err = conn:connect(connect_host, connect_port);
+ if not success and err ~= "timeout" then
+ log("warn", "s2s connect() to %s (%s:%d) failed: %s", host_session.to_host, connect_host, connect_port, err);
+ return false, err;
+ end
+
+ local cl = connlisteners_get("xmppserver");
+ conn = wrapclient(conn, connect_host, connect_port, cl, cl.default_mode or 1, hosts[from_host].ssl_ctx, false );
+ host_session.conn = conn;
+
+ -- Register this outgoing connection so that xmppserver_listener knows about it
+ -- otherwise it will assume it is a new incoming connection
+ cl.register_outgoing(conn, host_session);
+
+ local w = conn.write;
+ host_session.sends2s = function (t) log("debug", "sending: %s", tostring(t)); w(tostring(t)); end
+
+ conn.write(format([[<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' from='%s' to='%s' version='1.0' xml:lang='en'>]], from_host, to_host));
+ log("debug", "Connection attempt in progress...");
+ add_task(connect_timeout, function ()
+ if host_session.conn ~= conn or
+ host_session.type == "s2sout" or
+ host_session.connecting then
+ return; -- Ok, we're connect[ed|ing]
+ end
+ -- Not connected, need to close session and clean up
+ (host_session.log or log)("warn", "Destroying incomplete session %s->%s due to inactivity",
+ host_session.from_host or "(unknown)", host_session.to_host or "(unknown)");
+ host_session:close("connection-timeout");
+ end);
+ return true;