+ 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 ));
+
+ /* Fehler aufgetreten? */
+ 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 socket, connection and client structures */
+ c = Client_GetFromConn( idx );
+ if( c ) Client_DestroyNow( c );
+ io_close( My_Connections[idx].sock );
+ Init_Conn_Struct( idx );
+
+ /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
+ Conf_Server[Conf_GetServer( idx )].lasttry = time( NULL );
+ Conf_UnsetServer( idx );
+ return;
+ }
+
+ Conn_OPTION_DEL( &My_Connections[idx], CONN_ISCONNECTING );
+
+ 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 );
+}