+ 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 = Conn_GetClient(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 );
+}
+
+
+GLOBAL void
+Conn_Init( void )