ANSC_HANDLE
HttpScoMapWcso
(
    ANSC_HANDLE                 hThisObject,
    char*                       host,
    USHORT                      port,
    ANSC_HANDLE                 hReqContext,
    BOOL                        bUseTls
)
{
    ANSC_STATUS                     returnStatus = ANSC_STATUS_SUCCESS;
    PHTTP_SIMPLE_CLIENT_OBJECT      pMyObject    = (PHTTP_SIMPLE_CLIENT_OBJECT  )hThisObject;
    PHTTP_SIMPLE_CLIENT_PROPERTY    pProperty    = (PHTTP_SIMPLE_CLIENT_PROPERTY)&pMyObject->Property;
    PHTTP_WEBC_SESSION_OBJECT       pWebcSession = NULL;
    PSINGLE_LINK_ENTRY              pSLinkEntry  = NULL;
    ULONG                           ulHashIndex  = AnscHashString2(host, AnscSizeOfString(host), HTTP_SCO_WCSO_TABLE_SIZE);
    ULONG                           ulClientMode = pMyObject->ClientMode;
    ULONG                           ulSessFlags;

    AnscAcquireLock(&pMyObject->WcsoTableLock);

    pSLinkEntry = AnscSListGetFirstEntry(&pMyObject->WcsoTable[ulHashIndex]);

    while ( pSLinkEntry )
    {
        pWebcSession = ACCESS_HTTP_WEBC_SESSION_OBJECT(pSLinkEntry);
        pSLinkEntry  = AnscSListGetNextEntry(pSLinkEntry);

        if ( pWebcSession->bRemoveMe )
        {
            continue;
        }

        ulSessFlags = bUseTls? HTTP_WCSO_FLAG_tlsEnabled : 0;

        if ( pMyObject->ClientMode & HTTP_SCO_MODE_XSOCKET )
        {
            ulSessFlags |= HTTP_WCSO_FLAG_xsocketEnabled;
        }

        if ( pMyObject->ClientMode & HTTP_SCO_MODE_NOTIFY_ON_ALL_CONN_ONCE )
        {
            ulSessFlags |= HTTP_WCSO_FLAG_BspNotifyOnAllConnOnce;
        }

        if ( pWebcSession->MatchServer
                (
                    (ANSC_HANDLE)pWebcSession,
                    host,
                    port,
                    ulSessFlags
                ) )
        {
            pWebcSession->AcquireAccess((ANSC_HANDLE)pWebcSession);

            if ( pWebcSession->IsReadyToRequest((ANSC_HANDLE)pWebcSession) )
            {
                ANSC_IPV4_ADDRESS   ipAddr;

                AnscReleaseLock(&pMyObject->WcsoTableLock);

                ipAddr.Value    = pMyObject->GetCurPeerAddr((ANSC_HANDLE)pMyObject);

                if ( ipAddr.Value != 0 )
                {
                    pMyObject->ResetPeerAddrPick((ANSC_HANDLE)pMyObject, ipAddr.Value);
                }

                return  (ANSC_HANDLE)pWebcSession;
            }
            else
            {
                pWebcSession->ReleaseAccess((ANSC_HANDLE)pWebcSession);
            }
        }
    }

    AnscReleaseLock(&pMyObject->WcsoTableLock);

    return  pMyObject->AddWcso
            (
                (ANSC_HANDLE)pMyObject,
                host,
                port,
                hReqContext,
                bUseTls
            );
}
ANSC_STATUS
HttpWcsoConnect
    (
        ANSC_HANDLE                 hThisObject
    )
{
    ANSC_STATUS                     returnStatus     = ANSC_STATUS_SUCCESS;
    PHTTP_WEBC_SESSION_OBJECT       pMyObject        = (PHTTP_WEBC_SESSION_OBJECT     )hThisObject;
    PHTTP_WEBC_SESSION_PROPERTY     pProperty        = (PHTTP_WEBC_SESSION_PROPERTY   )&pMyObject->Property;
    PHTTP_BSP_INTERFACE             pBspIf           = (PHTTP_BSP_INTERFACE           )pMyObject->hBspIf;
    PANSC_SIMPLE_CLIENT_TCP_OBJECT  pTcpSimpleClient = (PANSC_SIMPLE_CLIENT_TCP_OBJECT)pMyObject->hTcpSimpleClient;
    PHTTP_SIMPLE_CLIENT_OBJECT      pHttpSco         = (PHTTP_SIMPLE_CLIENT_OBJECT    )pMyObject->hOwnerContext;
    ULONG                           ulSctoMode       = ANSC_SCTO_MODE_FOREIGN_BUFFER;
    PANSC_BUFFER_DESCRIPTOR         pBufferDesp      = NULL;
    HTTP_SCO_HOST_ADDRESSES         PeerAddr;
    BOOL                            bSameAddresses   = FALSE;
    ANSC_IPV4_ADDRESS               ipAddr           = {1,1,1,1};

    AnscStartupXsocketWrapper((ANSC_HANDLE)pMyObject);

    AnscZeroMemory(&PeerAddr, sizeof(HTTP_SCO_HOST_ADDRESSES));
    PeerAddr.NumPeers   = HTTP_SCO_MAX_RESOLVED_HOST;

    if ( pProperty->SessionFlags & HTTP_WCSO_FLAG_xsocketEnabled )
    {
        XsktResolveHostName2(pProperty->PeerName, ((PULONG)PeerAddr.PeerAddresses), (PeerAddr.NumPeers));
    }
    else
    {
        AnscResolveHostName2(pProperty->PeerName, ((PULONG)PeerAddr.PeerAddresses), (PeerAddr.NumPeers));
    }

#ifndef _ANSC_IPV6_COMPATIBLE_
    if ( PeerAddr.NumPeers == 0 )
    {
        returnStatus =
            pBspIf->Notify
                (
                    pBspIf->hOwnerContext,
                    HTTP_BSP_EVENT_CANNOT_RESOLVE_NAME,
                    0,
                    pMyObject->hBspReqContext,
                    NULL,
                    NULL
                );

        pMyObject->SessionState = HTTP_WCSO_STATE_FINISHED;

        return  ANSC_STATUS_BAD_NAME;
    }
    else
    {
        pHttpSco->SetPeerAddresses((ANSC_HANDLE)pHttpSco, (ANSC_HANDLE)&PeerAddr, &bSameAddresses);

        if ( bSameAddresses )
        {
            ipAddr.Value    = pHttpSco->GetCurPeerAddr((ANSC_HANDLE)pHttpSco);
            pHttpSco->ResetPeerAddrPick((ANSC_HANDLE)pHttpSco, ipAddr.Value);
        }
        else
        {
            pHttpSco->GetNextPeerAddr((ANSC_HANDLE)pHttpSco, &ipAddr.Value);
        }
    }
#endif
    returnStatus = pMyObject->ConnectPeer((ANSC_HANDLE)pMyObject, ipAddr.Value);

    if ( returnStatus != ANSC_STATUS_SUCCESS )
    {
        pMyObject->ConnectionFailed((ANSC_HANDLE)pMyObject);

        pMyObject->SessionState = HTTP_WCSO_STATE_FINISHED;

        return  ANSC_STATUS_FAILURE;
    }

    return  returnStatus;
}