ANSC_STATUS
HttpWcsoConnectPeer
    (
        ANSC_HANDLE                 hThisObject,
        ULONG                       ipAddrToConnect
    )
{
    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;
    BOOL                            bGotPeerAddr     = TRUE;
    ANSC_IPV4_ADDRESS               PeerAddr;
    PANSC_BUFFER_DESCRIPTOR         pRecvBdo         = (ANSC_HANDLE)NULL;
    ULONG                           ipAddr           = ipAddrToConnect;

TRY_NEXT:
    if ( ipAddr == 0 )
    {
        goto NO_MORE_TRY;
    }

    PeerAddr.Value  = ipAddr;

    pRecvBdo     = (PANSC_BUFFER_DESCRIPTOR)pTcpSimpleClient->GetBufferContext((ANSC_HANDLE)pTcpSimpleClient);
    pTcpSimpleClient->SetBufferContext((ANSC_HANDLE)pTcpSimpleClient, NULL, 0, NULL);

    if ( pRecvBdo )
    {
        AnscReleaseBdo(pRecvBdo);
        pRecvBdo    = NULL;
    }

    returnStatus = pTcpSimpleClient->Cancel((ANSC_HANDLE)pTcpSimpleClient);

    if ( pProperty->SessionFlags & HTTP_WCSO_FLAG_tlsEnabled )
    {
        ulSctoMode |= ANSC_SCTO_MODE_TLS_ENABLED;
    }

    if ( pProperty->SessionFlags & HTTP_WCSO_FLAG_xsocketEnabled )
    {
        ulSctoMode |= ANSC_SCTO_MODE_XSOCKET;
    }

    pTcpSimpleClient->SetHostAddress   ((ANSC_HANDLE)pTcpSimpleClient, pProperty->HostAddr.Dot   );
    pTcpSimpleClient->SetHostPort      ((ANSC_HANDLE)pTcpSimpleClient, pProperty->HostPort       );
    pTcpSimpleClient->SetPeerName      ((ANSC_HANDLE)pTcpSimpleClient, pProperty->PeerName       );
    pTcpSimpleClient->SetPeerAddress   ((ANSC_HANDLE)pTcpSimpleClient, PeerAddr.Dot              );
    pTcpSimpleClient->SetPeerPort      ((ANSC_HANDLE)pTcpSimpleClient, pProperty->PeerPort       );
    pTcpSimpleClient->SetMaxMessageSize((ANSC_HANDLE)pTcpSimpleClient, HTTP_WCSO_MAX_MESSAGE_SIZE);
    pTcpSimpleClient->SetMode          ((ANSC_HANDLE)pTcpSimpleClient, ulSctoMode                );

    //    fprintf(stderr, "<RT> %s: HTTP Client DeviceName = '%s'\n", __FUNCTION__, pHttpSco ? pHttpSco->Property.BindToDevice.DeviceName : "NULL");
    if( pHttpSco && pHttpSco->Property.BindToDevice.DeviceName[0] &&
        AnscSizeOfString(pHttpSco->Property.BindToDevice.DeviceName) < ANSC_OBJ_IF_NAME_SIZE)
    {
        pTcpSimpleClient->SetSocketBindToDevice ((ANSC_HANDLE)pTcpSimpleClient, TRUE);
        pTcpSimpleClient->SetSocketDeviceName   ((ANSC_HANDLE)pTcpSimpleClient, pHttpSco->Property.BindToDevice.DeviceName);
    }
    else 
    {
        pTcpSimpleClient->SetSocketBindToDevice ((ANSC_HANDLE)pTcpSimpleClient, FALSE);
        pTcpSimpleClient->SetSocketDeviceName   ((ANSC_HANDLE)pTcpSimpleClient, "");
    }

    returnStatus = pTcpSimpleClient->Engage((ANSC_HANDLE)pTcpSimpleClient);

NO_MORE_TRY:
    if ( returnStatus != ANSC_STATUS_SUCCESS )
    {
        if ( bGotPeerAddr )
        {
            BOOL                    bSucc   = FALSE;

            bSucc   = pHttpSco->GetNextPeerAddr((ANSC_HANDLE)pHttpSco, &ipAddr);

            if ( bSucc && ipAddr != 0 )
            {
                pMyObject->SessionState = HTTP_WCSO_STATE_INITIALIZED;

                goto TRY_NEXT;
            }
        }

        return  ANSC_STATUS_FAILURE;
    }
    else
    {
        pMyObject->SessionState = HTTP_WCSO_STATE_SERVER_CONNECTED;
    }

    pBufferDesp =
        (PANSC_BUFFER_DESCRIPTOR)AnscAllocateBdo
            (
                HTTP_WCSO_MAX_MESSAGE_SIZE,
                0,
                0
            );

    if ( pBufferDesp )
    {
        pTcpSimpleClient->SetBufferContext
            (
                (ANSC_HANDLE)pTcpSimpleClient,
                AnscBdoGetEndOfBlock(pBufferDesp),
                AnscBdoGetLeftSize  (pBufferDesp),
                (ANSC_HANDLE)pBufferDesp
            );
    }

    return returnStatus;
}
ANSC_STATUS
HttpWcsoClose
    (
        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;
    PANSC_SIMPLE_CLIENT_TCP_OBJECT  pTcpSimpleClient = (PANSC_SIMPLE_CLIENT_TCP_OBJECT)pMyObject->hTcpSimpleClient;
    PANSC_BUFFER_DESCRIPTOR         pBufferDesp      = (PANSC_BUFFER_DESCRIPTOR       )pTcpSimpleClient->GetBufferContext((ANSC_HANDLE)pTcpSimpleClient);
    PHTTP_WEBC_TRANS_OBJECT         pWebcTrans       = (PHTTP_WEBC_TRANS_OBJECT       )NULL;

    if ( pMyObject->SessionState == HTTP_WCSO_STATE_FINISHED )
    {
        return  ANSC_STATUS_SUCCESS;
    }
    else
    {
        pMyObject->SessionState = HTTP_WCSO_STATE_FINISHED;
    }

    AnscTrace("Http Webc Session Object is to be closed!\n");

    if ( !pMyObject->bRemoveMe )
    {
        pWebcTrans = (PHTTP_WEBC_TRANS_OBJECT)pMyObject->GetEndWcto((ANSC_HANDLE)pMyObject);

        if ( pWebcTrans )
        {
            if ( pWebcTrans->GetTransState((ANSC_HANDLE)pWebcTrans) != HTTP_WCTO_STATE_FINISHED )
            {
                returnStatus =
                    pWebcTrans->Abort
                        (
                            (ANSC_HANDLE)pWebcTrans,
                            HTTP_BSP_EVENT_TIMEOUT
                        );
            }

            pWebcTrans->ReleaseAccess((ANSC_HANDLE)pWebcTrans);
        }
    }

    pMyObject->DelAllWctos((ANSC_HANDLE)pMyObject);

    pTcpSimpleClient->Cancel((ANSC_HANDLE)pTcpSimpleClient);

    if ( pBufferDesp )
    {
        AnscFreeBdo((ANSC_HANDLE)pBufferDesp);
    }

    return  ANSC_STATUS_SUCCESS;
}