def wait_socks(sock_events, inmask=1, outmask=2, timeout=None):
    results = []
    for sock, mask in sock_events:
        if isinstance(sock, zmq.backend.Socket):
            mask = _check_events(sock, mask, inmask, outmask)
            if mask:
                results.append((sock, mask))
    if results:
        return results
    fd_map = {}
    fd_events = []
    for sock, mask in sock_events:
        if isinstance(sock, zmq.backend.Socket):
            fd = sock.getsockopt(zmq.FD)
        elif isinstance(sock, int):
            fd = sock
        else:
            fd = sock.fileno()
        fd_map[fd] = sock
        fd_events.append((fd, mask))
    while 1:
        started = time.time()
        active = descriptor.wait_fds(fd_events, inmask, outmask, timeout)
        if not active:
            return []
        results = []
        for fd, mask in active:
            sock = fd_map[fd]
            if isinstance(sock, zmq.backend.Socket):
                mask = _check_events(sock, mask, inmask, outmask)
                if not mask:
                    continue
            results.append((sock, mask))
        if results:
            return results
        timeout -= time.time() - started