INT
DWORKERTHREAD::QueueOverlappedAcceptEx(
    PDSOCKET                           ListenSocket,
    PDSOCKET                           AcceptSocket,
    LPVOID                             lpOutputBuffer,
    DWORD                              dwReceiveDataLength,
    DWORD                              dwLocalAddressLength,
    DWORD                              dwRemoteAddressLength,
    LPWSAOVERLAPPED                    UserOverlappedStruct,
    LPINT                              Errno
    ) {
    INT                       ReturnCode;
    PINTERNALOVERLAPPEDSTRUCT OverlappedStruct;

    ReturnCode = SOCKET_ERROR;
    *Errno = WSAENOBUFS;

    OverlappedStruct =
        gOverlappedManager->AllocateOverlappedStruct();
    if (OverlappedStruct){
        OverlappedStruct->iolOperationType = ACCEPT_EX;
        OverlappedStruct->iolSocket = ListenSocket->GetSocketHandle ();
        OverlappedStruct->iolProvider = ListenSocket->GetDProvider();
        OverlappedStruct->iolListenSocket = ListenSocket->GetProviderSocket ();
        OverlappedStruct->iolAcceptSocket = AcceptSocket->GetProviderSocket ();
        OverlappedStruct->iolUserOverlappedStruct = UserOverlappedStruct;
        OverlappedStruct->iolUserCompletionRoutine = NULL;
        OverlappedStruct->iolOutputBuffer = lpOutputBuffer;
        OverlappedStruct->iolOutputBufferLength = dwReceiveDataLength;
        OverlappedStruct->iolLocalAddressLength = dwLocalAddressLength;
        OverlappedStruct->iolRemoteAddressLength = dwRemoteAddressLength;

        AddOverlappedOperation(
            ListenSocket,
            OverlappedStruct);

        *Errno = WSA_IO_PENDING;
    } //if
    return(ReturnCode);
}
INT
DWORKERTHREAD::QueueOverlappedTransmitFile(
    PDSOCKET                            Socket,
    HANDLE                              hFile,
    DWORD                               nNumberOfBytesToWrite,
    DWORD                               nNumberOfBytesPerSend,
    LPWSAOVERLAPPED                     UserOverlappedStruct,
    LPTRANSMIT_FILE_BUFFERS             lpTransmitBuffers,
    DWORD                               dwReserved,
    LPINT                               Errno
    )
{
    INT                       ReturnCode;
    PINTERNALOVERLAPPEDSTRUCT OverlappedStruct;

    ReturnCode = SOCKET_ERROR;
    *Errno = WSAENOBUFS;

    OverlappedStruct =
        gOverlappedManager->AllocateOverlappedStruct();
    if (OverlappedStruct){
        OverlappedStruct->iolOperationType = TRANSMIT_FILE;
        OverlappedStruct->iolSocket = Socket->GetSocketHandle ();
        OverlappedStruct->iolProvider = Socket->GetDProvider();
        OverlappedStruct->iolProviderSocket = Socket->GetProviderSocket ();
        OverlappedStruct->iolUserOverlappedStruct = UserOverlappedStruct;
        OverlappedStruct->iolUserCompletionRoutine = NULL;
        OverlappedStruct->iolFileHandle = hFile;
        OverlappedStruct->iolBytesToWrite = nNumberOfBytesToWrite;
        OverlappedStruct->iolBytesPerSend = nNumberOfBytesPerSend;
        if (lpTransmitBuffers)
            OverlappedStruct->iolTransmitBuffers = *lpTransmitBuffers;
        else
            OverlappedStruct->iolTransmitBuffers.HeadLength =
                OverlappedStruct->iolTransmitBuffers.TailLength = 0;

        OverlappedStruct->iolReserved = dwReserved;
        AddOverlappedOperation(
            Socket,
            OverlappedStruct);

        *Errno = WSA_IO_PENDING;
    } //if
    return(ReturnCode);
}
INT
DWORKERTHREAD::QueueOverlappedSend(
    PDSOCKET                           Socket,
    LPWSABUF                           UserBuffers,
    DWORD                              UserBufferCount,
    LPDWORD                            UserBytesSent,
    DWORD                              UserFlags,
    LPWSAOVERLAPPED                    UserOverlappedStruct,
    LPWSAOVERLAPPED_COMPLETION_ROUTINE UserCompletionRoutine,
    LPWSATHREADID                      UserThreadId,
    LPINT           Errno
    )
/*++

Routine Description:

    this routine allocates an internal overlapped structure stores its
    arguments in the allocated structure and enqueues the structure for the
    worker thread to complet the I/O operation.

Arguments:

    Socket      - Socket object

    UserBuffers - The pointer to the user buffer(s).

    UserBufferCount - The number of user buffers.

    UserBytesSent - The pointer to the user BytesSent parameter.

    UserFlags - The user flags .

    UserOverlappedStruct - The user overlapped struct pointer.

    UserCompletionRoutine - The user overlapped completion routine.

    UserThreadId - The user thread ID.

    InternalBuffers - A pointer to our internal buffer(s).

    InternalBufferCount - The number of internal buffers.

    Errno - A pointer to the user errno parameter.

Return Value:

    NO_ERROR on success else a valid winsock2 error code.

--*/
{
    INT                       ReturnCode;
    PINTERNALOVERLAPPEDSTRUCT OverlappedStruct;

    ReturnCode = SOCKET_ERROR;
    *Errno = WSAENOBUFS;

    OverlappedStruct =
        gOverlappedManager->AllocateOverlappedStruct();
    if (OverlappedStruct){
        OverlappedStruct->iolOperationType = WSP_SEND;
        OverlappedStruct->iolSocket = Socket->GetSocketHandle ();
        OverlappedStruct->iolProvider = Socket->GetDProvider();
        OverlappedStruct->iolProviderSocket = Socket->GetProviderSocket ();
        OverlappedStruct->iolUserOverlappedStruct = UserOverlappedStruct;
        OverlappedStruct->iolUserCompletionRoutine = UserCompletionRoutine;
        OverlappedStruct->iolUserThreadId = *UserThreadId;
        if (UserBufferCount<=MAX_FAST_BUFS)
            memcpy (OverlappedStruct->iolUserBuffers, UserBuffers,
                        sizeof (WSABUF)*UserBufferCount);
        else {
            OverlappedStruct->iolpUserBuffers = new WSABUF[UserBufferCount];
            if (OverlappedStruct->iolpUserBuffers==NULL) {
                gOverlappedManager->FreeOverlappedStruct (
                        &OverlappedStruct->iolInternalOverlappedStruct);
                return ReturnCode;
            }
            memcpy (OverlappedStruct->iolpUserBuffers, UserBuffers,
                        sizeof (WSABUF)*UserBufferCount);
        }
        OverlappedStruct->iolUserBufferCount = UserBufferCount;
        OverlappedStruct->iolFlags = UserFlags;

        AddOverlappedOperation(
            Socket,
            OverlappedStruct);

        *Errno = WSA_IO_PENDING;

    } //if
    return(ReturnCode);
}
INT
DWORKERTHREAD::QueueOverlappedIoctl(
    PDSOCKET                           Socket,
    DWORD                              dwIoControlCode,
    LPVOID                             lpvInBuffer,
    DWORD                              cbInBuffer,
    LPVOID                             lpvOutBuffer,
    DWORD                              cbOutBuffer,
    LPDWORD                            lpcbBytesReturned,
    LPWSAOVERLAPPED                    UserOverlappedStruct,
    LPWSAOVERLAPPED_COMPLETION_ROUTINE UserCompletionRoutine,
    LPWSATHREADID                      UserThreadId,
    LPINT                              Errno
    )
/*++

Routine Description:

    this routine allocates an internal overlapped structure stores its
    arguments in the allocated structure and enqueues the structure for the
    worker thread to complet the I/O operation.

Arguments:

    Socket      - Socket object

    UserBuffers - The pointer to the user buffer(s).

    UserBufferCount - The number of user buffers.

    UserBytesRecvd - The pointer to the user BytesRecvd parameter.

    UserFlags - A pointer to the user flags argument.

    UserTo - A pointer to the user sockaddr structure.

    UserToLen - The length of the user sockaddr structure.

    UserOverlappedStruct - The user overlapped struct pointer.

    UserCompletionRoutine - The user overlapped completion routine.

    UserThreadId - The user thread ID.

    InternalBuffers - A pointer to our internal buffer(s).

    InternalBufferCount - The number of internal buffers.

    Errno - A pointer to the user errno parameter.

Return Value:

    NO_ERROR on success else a valid winsock2 error code.

--*/
{
    INT                       ReturnCode;
    PINTERNALOVERLAPPEDSTRUCT OverlappedStruct;

    ReturnCode = SOCKET_ERROR;
    *Errno = WSAENOBUFS;

    OverlappedStruct =
        gOverlappedManager->AllocateOverlappedStruct();
    if (OverlappedStruct){
        OverlappedStruct->iolOperationType = WSP_IOCTL;
        OverlappedStruct->iolSocket = Socket->GetSocketHandle ();
        OverlappedStruct->iolProvider = Socket->GetDProvider();
        OverlappedStruct->iolProviderSocket = Socket->GetProviderSocket ();
        OverlappedStruct->iolUserOverlappedStruct = UserOverlappedStruct;
        OverlappedStruct->iolUserCompletionRoutine = UserCompletionRoutine;
        OverlappedStruct->iolUserThreadId = *UserThreadId;
        OverlappedStruct->iolInputBuffer = lpvInBuffer;
        OverlappedStruct->iolInputBufferLength = cbInBuffer;
        OverlappedStruct->iolOutputBuffer = lpvOutBuffer;
        OverlappedStruct->iolOutputBufferLength = cbOutBuffer;
        OverlappedStruct->iolIoControlCode = dwIoControlCode;

        AddOverlappedOperation(
            Socket,
            OverlappedStruct);

        *Errno = WSA_IO_PENDING;
    } //if
    return(ReturnCode);
}