+static void server_login PARAMS((CONN_ID idx));
+
+static void cb_Read_Resolver_Result PARAMS(( int sock, UNUSED short what));
+static void cb_Connect_to_Server PARAMS(( int sock, UNUSED short what));
+static void cb_clientserver PARAMS((int sock, short what));
+
+static void
+cb_listen(int sock, short irrelevant)
+{
+ (void) irrelevant;
+ New_Connection( sock );
+}
+
+
+static void
+cb_connserver(int sock, UNUSED short what)
+{
+ int res, err;
+ socklen_t sock_len;
+ CLIENT *c;
+ CONN_ID idx = Socket2Index( sock );
+ if (idx <= NONE) {
+ LogDebug("cb_connserver wants to write on unknown socket?!");
+ io_close(sock);
+ return;
+ }
+
+ assert( what & IO_WANTWRITE);
+
+ /* connect() finished, get result. */
+ sock_len = sizeof( err );
+ res = getsockopt( My_Connections[idx].sock, SOL_SOCKET, SO_ERROR, &err, &sock_len );
+ assert( sock_len == sizeof( err ));
+
+ /* Error while connecting? */
+ if ((res != 0) || (err != 0)) {
+ if (res != 0)
+ Log(LOG_CRIT, "getsockopt (connection %d): %s!",
+ idx, strerror(errno));
+ else
+ Log(LOG_CRIT,
+ "Can't connect socket to \"%s:%d\" (connection %d): %s!",
+ My_Connections[idx].host,
+ Conf_Server[Conf_GetServer(idx)].port,
+ idx, strerror(err));
+
+ /* Clean up the CLIENT structure (to avoid silly log
+ * messages) and call Conn_Close() to do the rest. */
+ c = Client_GetFromConn(idx);
+ if (c)
+ Client_DestroyNow(c);
+
+ Conn_Close(idx, "Can't connect!", NULL, false);
+
+ return;
+ }
+
+ Conn_OPTION_DEL( &My_Connections[idx], CONN_ISCONNECTING );
+ server_login(idx);
+}
+
+
+static void
+server_login(CONN_ID idx)
+{
+ Log( LOG_INFO, "Connection %d with \"%s:%d\" established. Now logging in ...", idx,
+ My_Connections[idx].host, Conf_Server[Conf_GetServer( idx )].port );
+
+ io_event_setcb( My_Connections[idx].sock, cb_clientserver);
+ io_event_add( My_Connections[idx].sock, IO_WANTREAD|IO_WANTWRITE);
+
+ /* Send PASS and SERVER command to peer */
+ Conn_WriteStr( idx, "PASS %s %s", Conf_Server[Conf_GetServer( idx )].pwd_out, NGIRCd_ProtoID );
+ Conn_WriteStr( idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
+}
+
+
+static void
+cb_clientserver(int sock, short what)
+{
+ CONN_ID idx = Socket2Index( sock );
+ if (idx <= NONE) {
+#ifdef DEBUG
+ Log(LOG_WARNING, "WTF: cb_clientserver wants to write on unknown socket?!");
+#endif
+ io_close(sock);
+ return;
+ }
+
+ if (what & IO_WANTREAD)
+ Read_Request( idx );
+
+ if (what & IO_WANTWRITE)
+ Handle_Write( idx );
+}
+