WSAEVENT wsevent = WSACreateEvent();
THREADFUNC(irc_sockeventloop) {    char *data = (char*)callocm(513, sizeof(char));    int i = 0;    WSAEVENT wsevent = WSACreateEvent();    WSANETWORKEVENTS netevents;        WSAEventSelect(bot.sock, wsevent, (FD_CONNECT | FD_READ | FD_CLOSE));        while(1) {        if(WSAWaitForMultipleEvents(1, &wsevent, 0, WSA_INFINITE, 0) == WSA_WAIT_EVENT_0) {            WSAEnumNetworkEvents(bot.sock, wsevent, &netevents);            if(netevents.lNetworkEvents & FD_CONNECT) {                /* Connected. */                irc_start(1);            }            else if(netevents.lNetworkEvents & FD_READ) {                /* We've recieved some data, let's do something with it. */                i = irc_recv(data);                if(i == 1) {                    printf("RECV: %s\n", data);                    if(!irc_parseraw(data))                        make_warning("irc_parseraw failed.");                    memset(data, '\0', 513);                    bot.current_try = 0;                }                else if(i == -1) {                    make_error("Failed to allocate memory for recieved data.");                }            }            else if(netevents.lNetworkEvents & FD_CLOSE) {                /* We've been disconnected. */                if(bot.current_try <= bot.maxretry) {                    bot.current_try++;                    i = bot.current_try;                    irc_disconnect();                    bot.current_try = i;                    printf("Disconnected. Reconnecting in...\n");                    for(i = 60;i > 0;i--) {                        printf("%d\n", i);                        waits(1);                    }                    memset(data, '\0', 513);                    printf("Connecting to %s:%d... ", bot.servaddr, bot.servport);                    if((i = irc_connect(bot.servaddr, bot.servport)) != 0) {                        printf("Failed to connect. ERROR %d:%d\n\n", WSAGetLastError(), i);                        log_write("Connecting failed with code: %d:%d", WSAGetLastError(), i);                        printf("Press Enter key to continue.");                        getchar();                        freem(data);                        exit(1);                    }                    printf("Connected.\n");                    WSAEventSelect(bot.sock, wsevent, (FD_CONNECT | FD_READ | FD_CLOSE));                }                else {                    printf("Disconnected.");                    break;                }            }        }    }        freem(data);    return 0;}