int CheckSocket(int s, int flags) {
    fd_set rfds, wfds;
    struct timeval tv, *tvptr;
    tv.tv_sec = tv.tv_usec = 0;

    tvptr = (flags & SOCKLIB_CHK_IMMED) ? &tv : 0;

    FD_ZERO(&rfds);
    FD_ZERO(&wfds);

    if(flags & SOCKLIB_CHK_READ) {
        FD_SET(s, &rfds);
        select(s+1, &rfds, 0, 0, tvptr);
        return FD_ISSET(s, &rfds) ? 1 : 0;
    } else if(flags & SOCKLIB_CHK_WRITE) {
        FD_SET(s, &wfds);
        select(s+1, 0, &wfds, 0, tvptr);
        return FD_ISSET(s, &wfds) ? 1 : 0;
    }

    return -1;
}