HRESULT CALLBACK Receive1(
    __in HRESULT hr, 
    __in WS_CALLBACK_MODEL callbackModel, 
    __in void* state, 
    __inout WS_ASYNC_OPERATION* next, 
    __in_opt const WS_ASYNC_CONTEXT* asyncContext, 
    __in_opt WS_ERROR* error)
{
    UNREFERENCED_PARAMETER(callbackModel);

    RECEIVE_STATE* receiveState = (RECEIVE_STATE*) state;
    if (FAILED(hr))
    {
        return hr;
    }

    // Create a listener
    hr = WsCreateListener(WS_CHANNEL_TYPE_DUPLEX_SESSION, WS_TCP_CHANNEL_BINDING, NULL, 0, NULL, &receiveState->listener, error);
    if (FAILED(hr))
    {
        return hr;
    }

    // Open listener using TCP duplex session
    static const WS_STRING uri = WS_STRING_VALUE(L"net.tcp://localhost/example");
    next->function = Receive2;
    return WsOpenListener(receiveState->listener, &uri, asyncContext, error);
}
// Main entry point
int __cdecl wmain()
{
    
    HRESULT hr = S_OK;
    WS_ERROR* error = NULL;
    WS_CHANNEL* channel = NULL;
    WS_LISTENER* listener = NULL;
    WS_HEAP* heap = NULL;
    WS_MESSAGE* message = NULL;
    
    // Create an error object for storing rich error information
    hr = WsCreateError(
        NULL, 
        0, 
        &error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a listener
    hr = WsCreateListener(
        WS_CHANNEL_TYPE_DUPLEX_SESSION, 
        WS_NAMEDPIPE_CHANNEL_BINDING, 
        NULL, 
        0, 
        NULL, 
        &listener, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a channel suitable for the listener
    hr = WsCreateChannelForListener(
        listener, 
        NULL, 
        0, 
        &channel, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Open listener using named pipe duplex session
    WS_STRING uri;
    uri.chars = L"net.pipe://localhost/example";
    uri.length = (ULONG)::wcslen(uri.chars);
    hr = WsOpenListener(
        listener, 
        &uri, 
        NULL, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    
    // Accept a channel from the client
    hr = WsAcceptChannel(listener, channel, NULL, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    hr = WsCreateMessageForChannel(
        channel,
        NULL, 
        0, 
        &message, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a heap to store deserialized data
    hr = WsCreateHeap(
        /*maxSize*/ 2048, 
        /*trimSize*/ 512, 
        NULL, 
        0, 
        &heap, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Receive messages
    for (;;)
    {
            // Receive the message start (headers)
            hr = WsReadMessageStart(
                channel, 
                message, 
                NULL, 
                error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        
            if (hr == WS_S_END)
            {
                // No more messages
                break;
            }
        
            // Get action value
            WS_XML_STRING receivedAction;
            hr = WsGetHeader(
                message, 
                WS_ACTION_HEADER, 
                WS_XML_STRING_TYPE,
                WS_READ_REQUIRED_VALUE, 
                NULL, 
                &receivedAction, 
                sizeof(receivedAction), 
                error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        
            // Make sure action is what we expect
            hr = WsXmlStringEquals(
                &receivedAction, 
                PurchaseOrder_wsdl.messages.PurchaseOrder.action, 
                error);
            
            if (hr != S_OK)
            {
                hr = WS_E_ENDPOINT_ACTION_NOT_SUPPORTED;
                goto Exit;
            }
        
        
            // Get the reader for the body
            WS_XML_READER* reader;
            hr = WsGetMessageProperty(
                message, 
                WS_MESSAGE_PROPERTY_BODY_READER, 
                &reader, 
                sizeof(reader), 
                error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        
            // Stream in the body data
            for (;;)
            {
                    // Read purchase order into heap, if there are any more.
                    _PurchaseOrderType* purchaseOrder;
                    hr = WsReadElement(
                        reader, 
                        &PurchaseOrder_wsdl.globalElements.PurchaseOrderType, 
                        WS_READ_OPTIONAL_POINTER, 
                        heap, 
                        &purchaseOrder, 
                        sizeof(purchaseOrder), 
                        error);
            if (FAILED(hr))
            {
                goto Exit;
            }
            
                    // NULL indicates no more purchase orders
                    if (purchaseOrder == NULL)
                    {
                        break;
                    }
            
                    // Print out purchase order contents
                    wprintf(L"%ld, %s\n", 
                        purchaseOrder->quantity, 
                        purchaseOrder->productName);
            
                    // Free purchase order
                    hr = WsResetHeap(
                        heap, 
                        error);
            if (FAILED(hr))
            {
                goto Exit;
            }
            }
        
            // Read the end of the message
            hr = WsReadMessageEnd(
                channel, 
                message, 
                NULL, 
                error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        
            hr = WsResetMessage(
                message, 
                error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    }
    
Exit:
    if (FAILED(hr))
    {
        // Print out the error
        PrintError(hr, error);
    }
    
    if (channel != NULL)
    {
        // Close the channel
        WsCloseChannel(channel, NULL, error);
    }
    if (listener != NULL)
    {
        // Close the listener if it was opened
        WsCloseListener(listener, NULL, error);
    }
    if (channel != NULL)
    {
        WsFreeChannel(channel);
    }
    if (listener != NULL)
    {
        WsFreeListener(listener);
    }
    if (message != NULL)
    {
        WsFreeMessage(message);
    }
    
    
    if (heap != NULL)
    {
        WsFreeHeap(heap);
    }
    if (error != NULL)
    {
        WsFreeError(error);
    }
    fflush(stdout);
    return SUCCEEDED(hr) ? 0 : -1;
}
// Main entry point
int __cdecl wmain()
{
    
    HRESULT hr = S_OK;
    WS_ERROR* error = NULL;
    WS_MESSAGE* requestMessage = NULL;
    WS_MESSAGE* replyMessage = NULL;
    WS_CHANNEL* channel = NULL;
    WS_LISTENER* listener = NULL;
    WS_HEAP* heap = NULL;
    
    // declare and initialize an Windows SSPI transport security binding
    WS_TCP_SSPI_TRANSPORT_SECURITY_BINDING sspiBinding = {}; // zero out the struct
    sspiBinding.binding.bindingType = WS_TCP_SSPI_TRANSPORT_SECURITY_BINDING_TYPE; // set the binding type
    
    // declare and initialize the array of all security bindings
    WS_SECURITY_BINDING* securityBindings[1] = { &sspiBinding.binding };
    
    // declare and initialize the security description
    WS_SECURITY_DESCRIPTION securityDescription = {}; // zero out the struct
    securityDescription.securityBindings = securityBindings;
    securityDescription.securityBindingCount = WsCountOf(securityBindings);
    
    // Create an error object for storing rich error information
    hr = WsCreateError(
        NULL, 
        0, 
        &error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a heap to store deserialized data
    hr = WsCreateHeap(
        /*maxSize*/ 2048, 
        /*trimSize*/ 512, 
        NULL, 
        0, 
        &heap, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a listener
    hr = WsCreateListener(
        WS_CHANNEL_TYPE_DUPLEX_SESSION, 
        WS_TCP_CHANNEL_BINDING, 
        NULL, 
        0, 
        &securityDescription, 
        &listener, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a channel suitable for the listener
    hr = WsCreateChannelForListener(
        listener, 
        NULL, 
        0, 
        &channel, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Open listener using TCP duplex session
    WS_STRING uri;
    uri.chars = L"net.tcp://localhost/example";
    uri.length = (ULONG)::wcslen(uri.chars);
    hr = WsOpenListener(
        listener, 
        &uri, 
        NULL, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    
    // Accept a channel from the client
    hr = WsAcceptChannel(listener, channel, NULL, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    hr = WsCreateMessageForChannel(
        channel,
        NULL, 
        0, 
        &requestMessage, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    hr = WsCreateMessageForChannel(
        channel,
        NULL, 
        0, 
        &replyMessage, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Receive requests / send replies
    for (;;)
    {
        // Set up the descriptions of the expected messages. We expect either a purchase order
        // or a request for order status.
        const WS_MESSAGE_DESCRIPTION* requestMessageDescriptions[] =
        {
            &PurchaseOrder_wsdl.messages.PurchaseOrder,   // contains a _PurchaseOrderType in the body
            &PurchaseOrder_wsdl.messages.GetOrderStatus,  // contains a GetOrderStatus in the body
        };
        
        // Receive the message and deserialize the element of the body into the appropriate
        // structure, based on the message descriptions.  The value of the body will be
        // allocated in the specified WS_HEAP, and are valid until WsResetHeap is called.
        void* requestBodyPointer;
        ULONG indexOfMatchedMessageDescription;
        
        hr = WsReceiveMessage(channel, requestMessage, requestMessageDescriptions, WsCountOf(requestMessageDescriptions),
            WS_RECEIVE_OPTIONAL_MESSAGE, WS_READ_REQUIRED_POINTER, heap,
            &requestBodyPointer, sizeof(requestBodyPointer), &indexOfMatchedMessageDescription, NULL, error);
        
        if (hr == WS_S_END)
        {
            // No more messages on channel
            break;
        }
        
        // Process the request, and generate the reply
        const WS_MESSAGE_DESCRIPTION* replyMessageDescription = NULL;
        const void* replyBodyPointer = NULL;
        ULONG replyBodySize = 0;
        _OrderConfirmationType orderConfirmation;
        _GetOrderStatusResponseType getOrderStatusResponse;
        
        if (SUCCEEDED(hr))
        {
            // Get the message description that matched
            const WS_MESSAGE_DESCRIPTION* requestMessageDescription = requestMessageDescriptions[indexOfMatchedMessageDescription];
        
            if (requestMessageDescription == &PurchaseOrder_wsdl.messages.PurchaseOrder)
            {
                // The message was a purchase order.  Get the pointer to the deserialized value.
                _PurchaseOrderType* purchaseOrder = (_PurchaseOrderType*)requestBodyPointer;
        
                // Print out purchase order contents
                wprintf(L"%d, %s\n",
                    purchaseOrder->quantity,
                    purchaseOrder->productName);
        
                // Initialize order confirmation data
                orderConfirmation.expectedShipDate = L"1/1/2006";
                orderConfirmation.orderID = 123;
        
                // Setup up reply message
                replyMessageDescription = &PurchaseOrder_wsdl.messages.OrderConfirmation;
                replyBodyPointer = &orderConfirmation;
                replyBodySize = sizeof(orderConfirmation);
            }
            else if (requestMessageDescription == &PurchaseOrder_wsdl.messages.GetOrderStatus)
            {
                // The message was a order status request.  Get the pointer to the deserialized value.
                _GetOrderStatusType* getOrderStatus = (_GetOrderStatusType*)requestBodyPointer;
        
                // Generate a fault if we don't recognize the order ID
                if (getOrderStatus->orderID != 123)
                {
                    // Fill out details about the fault
                    _OrderNotFoundFaultType orderNotFound;
                    orderNotFound.orderID = getOrderStatus->orderID;
                    
                    static const WS_XML_STRING _faultDetailName = WS_XML_STRING_VALUE("OrderNotFound");
                    static const WS_XML_STRING _faultDetailNs = WS_XML_STRING_VALUE("http://example.com");
                    static const WS_XML_STRING _faultAction = WS_XML_STRING_VALUE("http://example.com/fault");
                    static const WS_ELEMENT_DESCRIPTION _faultElementDescription = 
                    { 
                        (WS_XML_STRING*)&_faultDetailName, 
                        (WS_XML_STRING*)&_faultDetailNs, 
                        WS_UINT32_TYPE, 
                        NULL 
                    };
                    static const WS_FAULT_DETAIL_DESCRIPTION orderNotFoundFaultTypeDescription = 
                    { 
                        (WS_XML_STRING*)&_faultAction, 
                        (WS_ELEMENT_DESCRIPTION*)&_faultElementDescription 
                    };
                    
                    // Set fault detail information in the error object
                    hr = WsSetFaultErrorDetail(
                        error,
                        &orderNotFoundFaultTypeDescription,
                        WS_WRITE_REQUIRED_VALUE,
                        &orderNotFound,
                        sizeof(orderNotFound));
                    
                    if (FAILED(hr))
                    {
                        goto Exit;
                    }
                    
                    // Add an error string to the error object.  This string will
                    // be included in the fault that is sent.
                    static const WS_STRING errorMessage = WS_STRING_VALUE(L"Invalid order ID");
                    hr = WsAddErrorString(error, &errorMessage);
                    
                    if (FAILED(hr))
                    {
                        goto Exit;
                    }
        
                    // Use a failure code to indicate that a fault should be sent
                    hr = E_FAIL;
                }
                else
                {
                    // Initialize the order status response
                    getOrderStatusResponse.orderID = getOrderStatus->orderID;
                    getOrderStatusResponse.status = L"Pending";
        
                    // Specify which message description to use for reply
                    replyMessageDescription = &PurchaseOrder_wsdl.messages.GetOrderStatusResponse;
                    replyBodyPointer = &getOrderStatusResponse;
                    replyBodySize = sizeof(getOrderStatusResponse);
                }
            }
        }
        
        // If there was an error receiving the message
        if (FAILED(hr))
        {
            // Send a fault in the body of the reply message.  The information
            // accumulated in the error object is used to populate the fault.
            // The error code is not transmitted but instead is used to
            // generate an error string if no error strings are present in the
            // error object.
            hr = WsSendFaultMessageForError(
                channel,
                replyMessage,
                error,
                hr,
                WS_FULL_FAULT_DISCLOSURE,
                requestMessage,
                NULL,
                error);
        
            if (FAILED(hr))
            {
                goto Exit;
            }
        
            // Reset the error so it can be used again
            hr = WsResetError(error);
            if (FAILED(hr))
            {
                goto Exit;
            }
        
            // Reset the reply message so it can be used again
            hr = WsResetMessage(replyMessage, error);
            if (FAILED(hr))
            {
                goto Exit;
            }
        }
        else
        {
            // Send a reply message
            hr = WsSendReplyMessage(
                channel,
                replyMessage,
                replyMessageDescription,
                WS_WRITE_REQUIRED_VALUE,
                replyBodyPointer,
                replyBodySize,
                requestMessage,
                NULL,
                error);
        
            if (FAILED(hr))
            {
                goto Exit;
            }
        
            // Reset the reply message so it can be used again
            hr = WsResetMessage(replyMessage, error);
            if (FAILED(hr))
            {
                goto Exit;
            }
        }
        
        // Reset the request message so it can be used again
        hr = WsResetMessage(requestMessage, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        
        // Reset the heap, which will free any allocations made on it
        hr = WsResetHeap(heap, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    }
    
Exit:
    if (FAILED(hr))
    {
        // Print out the error
        PrintError(hr, error);
    }
    
    if (listener != NULL)
    {
        // Close the listener if it was opened
        WsCloseListener(listener, NULL, error);
    }
    if (listener != NULL)
    {
        WsFreeListener(listener);
    }
    
    if (channel != NULL)
    {
        // Close the channel
        WsCloseChannel(channel, NULL, error);
    }
    if (channel != NULL)
    {
        WsFreeChannel(channel);
    }
    
    if (requestMessage != NULL)
    {
        WsFreeMessage(requestMessage);
    }
    if (replyMessage != NULL)
    {
        WsFreeMessage(replyMessage);
    }
    
    
    if (error != NULL)
    {
        WsFreeError(error);
    }
    if (heap != NULL)
    {
        WsFreeHeap(heap);
    }
    fflush(stdout);
    return SUCCEEDED(hr) ? 0 : -1;
}
// Main entry point
int __cdecl wmain()
{

    HRESULT hr = S_OK;
    WS_ERROR* error = NULL;
    WS_CHANNEL* channel = NULL;
    WS_LISTENER* listener = NULL;
    WS_HEAP* heap = NULL;
    WS_MESSAGE* requestMessage = NULL;
    WS_MESSAGE* replyMessage = NULL;
    static const WS_STRING uri = WS_STRING_VALUE(L"http://+:80/example");

    // Create an error object for storing rich error information
    hr = WsCreateError(
             NULL,
             0,
             &error);
    if (FAILED(hr))
    {
        goto Exit;
    }

    // Create a listener
    hr = WsCreateListener(
             WS_CHANNEL_TYPE_REPLY,
             WS_HTTP_CHANNEL_BINDING,
             NULL, 0,
             NULL,
             &listener,
             error);
    if (FAILED(hr))
    {
        goto Exit;
    }

    // Open listener
    hr = WsOpenListener(
             listener,
             &uri,
             NULL,
             error);
    if (FAILED(hr))
    {
        goto Exit;
    }

    // Set up a property indicating streamined input and output
    WS_TRANSFER_MODE transferMode = WS_STREAMED_TRANSFER_MODE;
    WS_CHANNEL_PROPERTY transferModeProperty;
    transferModeProperty.id = WS_CHANNEL_PROPERTY_TRANSFER_MODE;
    transferModeProperty.value = &transferMode;
    transferModeProperty.valueSize = sizeof(transferMode);

    // Create a channel suitable for accepting from the listener
    hr = WsCreateChannelForListener(
             listener,
             &transferModeProperty,
             1,
             &channel,
             error);
    if (FAILED(hr))
    {
        goto Exit;
    }

    hr = WsCreateMessageForChannel(
             channel,
             NULL,
             0,
             &requestMessage,
             error);
    if (FAILED(hr))
    {
        goto Exit;
    }

    hr = WsCreateMessageForChannel(
             channel,
             NULL,
             0,
             &replyMessage,
             error);
    if (FAILED(hr))
    {
        goto Exit;
    }

    // Create a heap to store deserialized data
    hr = WsCreateHeap(
             /*maxSize*/ 2048,
             /*trimSize*/ 512,
             NULL,
             0,
             &heap,
             error);
    if (FAILED(hr))
    {
        goto Exit;
    }


    // Receive messages and send replies
    for (int i = 0; i < 10; i++)
    {
        // Accept a channel from the client
        hr = WsAcceptChannel(listener, channel, NULL, error);
        if (FAILED(hr))
        {
            goto Exit;
        }

        // Receive the message start (headers)
        hr = WsReadMessageStart(
                 channel,
                 requestMessage,
                 NULL,
                 error);
        if (FAILED(hr))
        {
            goto Exit;
        }

        // Get action value
        WS_XML_STRING receivedAction;
        hr = WsGetHeader(
                 requestMessage,
                 WS_ACTION_HEADER,
                 WS_XML_STRING_TYPE,
                 WS_READ_REQUIRED_VALUE, NULL,
                 &receivedAction,
                 sizeof(receivedAction),
                 error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        // Make sure action is what we expect
        hr = WsXmlStringEquals(
                 &receivedAction,
                 PurchaseOrder_wsdl.messages.PurchaseOrder.action,
                 error);

        if (hr != S_OK)
        {
            hr = WS_E_ENDPOINT_ACTION_NOT_SUPPORTED;
            goto Exit;
        }

        // Initialize the reply message based on the request
        hr = WsInitializeMessage(
                 replyMessage,
                 WS_REPLY_MESSAGE,
                 requestMessage,
                 error);
        if (FAILED(hr))
        {
            goto Exit;
        }

        // Write the start of the reply message (headers)
        hr = WsWriteMessageStart(
                 channel,
                 replyMessage,
                 NULL,
                 error);
        if (FAILED(hr))
        {
            goto Exit;
        }

        // Read the contents of the request body, and send response body
        for (;;)
        {
            // Make sure we have at least one purchase order buffered in the request message.
            // Each purchase order may be up to 1024 bytes in size.
            hr = WsFillBody(
                     requestMessage,
                     1024,
                     NULL,
                     error);
            if (FAILED(hr))
            {
                goto Exit;
            }
            // Deserialize purchase order into heap (if any more)
            _PurchaseOrderType* purchaseOrder;
            hr = WsReadBody(
                     requestMessage,
                     &PurchaseOrder_wsdl.globalElements.PurchaseOrderType,
                     WS_READ_OPTIONAL_POINTER,
                     heap,
                     &purchaseOrder,
                     sizeof(purchaseOrder),
                     error);
            if (FAILED(hr))
            {
                goto Exit;
            }

            // NULL indicates no more purchase orders
            if (purchaseOrder == NULL)
            {
                break;
            }

            // Print out purchase order contents
            wprintf(L"%ld, %s\n",
                    purchaseOrder->quantity,
                    purchaseOrder->productName);

            // Serialize a confirmation into the reply message
            _OrderConfirmationType orderConfirmation;
            orderConfirmation.expectedShipDate = L"1/1/2006";
            orderConfirmation.orderID = 123;

            hr = WsWriteBody(
                     replyMessage,
                     &PurchaseOrder_wsdl.globalElements.OrderConfirmationType,
                     WS_WRITE_REQUIRED_VALUE,
                     &orderConfirmation,
                     sizeof(orderConfirmation),
                     error);
            if (FAILED(hr))
            {
                goto Exit;
            }

            // Flush the confirmation data if at least 4096 bytes have been accumulated
            hr = WsFlushBody(
                     replyMessage,
                     4096,
                     NULL,
                     error);
            if (FAILED(hr))
            {
                goto Exit;
            }

            // Free purchase order
            hr = WsResetHeap(
                     heap,
                     error);
            if (FAILED(hr))
            {
                goto Exit;
            }
        }

        // Read the end of the message
        hr = WsReadMessageEnd(
                 channel,
                 requestMessage,
                 NULL,
                 error);
        if (FAILED(hr))
        {
            goto Exit;
        }

        // Write the end of the message
        hr = WsWriteMessageEnd(
                 channel,
                 replyMessage,
                 NULL,
                 error);
        if (FAILED(hr))
        {
            goto Exit;
        }

        // Reset the message so it can be used again
        hr = WsResetMessage(
                 requestMessage,
                 error);
        if (FAILED(hr))
        {
            goto Exit;
        }

        // Reset the message so it can be used again
        hr = WsResetMessage(
                 replyMessage,
                 error);
        if (FAILED(hr))
        {
            goto Exit;
        }

        if (channel != NULL)
        {
            // Close the channel
            WsCloseChannel(channel, NULL, error);
        }

        // Reset the channel so it can be used again
        hr = WsResetChannel(
                 channel,
                 error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    }

Exit:
    if (FAILED(hr))
    {
        // Print out the error
        PrintError(hr, error);
    }

    if (channel != NULL)
    {
        // Close the channel
        WsCloseChannel(channel, NULL, error);
    }
    if (listener != NULL)
    {
        // Close the listener if it was opened
        WsCloseListener(listener, NULL, error);
    }
    if (channel != NULL)
    {
        WsFreeChannel(channel);
    }
    if (listener != NULL)
    {
        WsFreeListener(listener);
    }
    if (requestMessage != NULL)
    {
        WsFreeMessage(requestMessage);
    }
    if (replyMessage != NULL)
    {
        WsFreeMessage(replyMessage);
    }


    if (heap != NULL)
    {
        WsFreeHeap(heap);
    }
    if (error != NULL)
    {
        WsFreeError(error);
    }
    fflush(stdout);
    return SUCCEEDED(hr) ? 0 : -1;
}
// Main entry point
int __cdecl wmain()
{
    
    HRESULT hr = S_OK;
    WS_ERROR* error = NULL;
    WS_MESSAGE* message = NULL;
    WS_CHANNEL* channel = NULL;
    WS_LISTENER* listener = NULL;
    WS_HEAP* heap = NULL;
    
    // Create an error object for storing rich error information
    hr = WsCreateError(
        NULL, 
        0, 
        &error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a heap to store deserialized data
    hr = WsCreateHeap(
        /*maxSize*/ 2048, 
        /*trimSize*/ 512, 
        NULL, 
        0, 
        &heap, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a listener
    hr = WsCreateListener(
        WS_CHANNEL_TYPE_DUPLEX, 
        WS_UDP_CHANNEL_BINDING, 
        NULL, 
        0, 
        NULL, 
        &listener, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Open listener at address
    WS_STRING uri;
    uri.chars = L"soap.udp://localhost:809";
    uri.length = (ULONG)wcslen(uri.chars);
    
    hr = WsOpenListener(
        listener, 
        &uri, 
        NULL, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    hr = WsCreateChannelForListener(
        listener, 
        NULL, 
        0, 
        &channel, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    hr = WsCreateMessageForChannel(
        channel,
        NULL, 
        0, 
        &message, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    
    for (ULONG i = 0; i < 5; i++)
    {
        // Accept a channel from the client
        hr = WsAcceptChannel(listener, channel, NULL, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        
        // Set up the descriptions of the expected messages.  In this case, we only expect purchase orders.
        const WS_MESSAGE_DESCRIPTION* messageDescriptions[] = {&PurchaseOrder_wsdl.messages.PurchaseOrder};
        
        // Receive the message and deserialize the element of the body into a _PurchaseOrderType structure.
        // The values of the fields of the _PurchaseOrderType structure are allocated in the specified
        // WS_HEAP, and are valid until WsResetHeap is called.
        _PurchaseOrderType purchaseOrder;
        hr = WsReceiveMessage(channel, message, 
            messageDescriptions, WsCountOf(messageDescriptions),
            WS_RECEIVE_OPTIONAL_MESSAGE, WS_READ_REQUIRED_VALUE, heap, 
            &purchaseOrder, sizeof(purchaseOrder), NULL, NULL, error);
        
        if (hr == WS_S_END)
        {
            // No more messages on channel
            break;
        }
        
        if (FAILED(hr))
        {
            goto Exit;
        }
        
        // Print out purchase order contents
        wprintf(L"%ld, %s\n", 
            purchaseOrder.quantity, 
            purchaseOrder.productName);
        
        // Reset the message so it can be used again
        hr = WsResetMessage(
                message,
                error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        
        // Reset the heap, which will free any allocations made on it
        hr = WsResetHeap(
                heap,
                error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        
        hr = WsCloseChannel(channel, NULL, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        
        hr = WsResetChannel(channel, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    }
    
Exit:
    if (FAILED(hr))
    {
        // Print out the error
        PrintError(hr, error);
    }
    
    if (channel != NULL)
    {
        // Close the channel
        WsCloseChannel(channel, NULL, error);
    }
    if (channel != NULL)
    {
        WsFreeChannel(channel);
    }
    
    if (listener != NULL)
    {
        // Close the listener if it was opened
        WsCloseListener(listener, NULL, error);
    }
    if (listener != NULL)
    {
        WsFreeListener(listener);
    }
    
    if (message != NULL)
    {
        WsFreeMessage(message);
    }
    
    
    if (error != NULL)
    {
        WsFreeError(error);
    }
    if (heap != NULL)
    {
        WsFreeHeap(heap);
    }
    fflush(stdout);
    return SUCCEEDED(hr) ? 0 : -1;
}
// Messages are relayed from the sender thread to the receiver thread by this thread
DWORD WINAPI RelayThread(void* parameter)
{
    HRESULT hr;
    THREADINFO* threadInfo = (THREADINFO*)parameter;
    HANDLE relayReadyEvent = threadInfo->event;
    WS_ERROR* error = NULL;
    WS_CHANNEL* sendChannel = NULL;
    WS_CHANNEL* receiveChannel = NULL;
    WS_MESSAGE* sendMessage = NULL;
    WS_MESSAGE* receiveMessage = NULL;
    WS_LISTENER* listener = NULL;
    
    // Create an error object for storing rich error information
    hr = WsCreateError(
        NULL, 
        0, 
        &error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a listener
    hr = WsCreateListener(WS_CHANNEL_TYPE_DUPLEX_SESSION, WS_TCP_CHANNEL_BINDING, NULL, 0, NULL, &listener, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Open listener using TCP duplex session
    hr = WsOpenListener(listener, &relayUrl, NULL, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a TCP duplex session channel
    hr = WsCreateChannel(WS_CHANNEL_TYPE_DUPLEX_SESSION,
        WS_TCP_CHANNEL_BINDING, NULL, 0, NULL, &sendChannel, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Initialize address of service
    WS_ENDPOINT_ADDRESS address;
    address.url = serviceUrl;
    address.headers = NULL;
    address.extensions = NULL;
    address.identity = NULL;
    
    // Open channel to address
    hr = WsOpenChannel(sendChannel, &address, NULL, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a channel
    hr = WsCreateChannelForListener(listener, NULL, 0, &receiveChannel, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Notify that thread is ready
    threadInfo->hr = NOERROR;
    SetEvent(relayReadyEvent);
    
    // Accept a channel from the client
    hr = WsAcceptChannel(listener, receiveChannel, NULL, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    hr = WsCreateMessageForChannel(
        receiveChannel,
        NULL, 
        0, 
        &receiveMessage, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    hr = WsCreateMessageForChannel(
        sendChannel,
        NULL, 
        0, 
        &sendMessage, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Forward all messages
    for (;;)
    {
        // Receive start of message (headers)
        hr = WsReadMessageStart(receiveChannel, receiveMessage, NULL, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        if (hr == WS_S_END)
        {
            // No more messages on this channel
            break;
        }
    
        // Copy headers from received message
        hr = WsInitializeMessage(sendMessage, WS_DUPLICATE_MESSAGE, receiveMessage, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        
        // Send message start (headers)
        hr = WsWriteMessageStart(sendChannel, sendMessage, NULL, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Get reader for receive message
        WS_XML_READER* xmlReader;
        hr = WsGetMessageProperty(receiveMessage, WS_MESSAGE_PROPERTY_BODY_READER, &xmlReader, sizeof(xmlReader), error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Get writer for send message
        WS_XML_WRITER* xmlWriter;
        hr = WsGetMessageProperty(sendMessage, WS_MESSAGE_PROPERTY_BODY_WRITER, &xmlWriter, sizeof(xmlWriter), error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Read and write contents of body
        for (;;)
        {
            const WS_XML_NODE* node;
            hr = WsGetReaderNode(xmlReader, &node, error);
            if (FAILED(hr))
            {
                goto Exit;
            }
    
            if (node->nodeType == WS_XML_NODE_TYPE_END_ELEMENT)
            {
                break;
            }
    
            hr = WsCopyNode(xmlWriter, xmlReader, error);
            if (FAILED(hr))
            {
                goto Exit;
            }
        }
    
        // Receive message end
        hr = WsReadMessageEnd(receiveChannel, receiveMessage, NULL, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Send message end
        hr = WsWriteMessageEnd(sendChannel, sendMessage, NULL, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Reset message to enable reuse
        hr = WsResetMessage(sendMessage, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Reset message to enable reuse
        hr = WsResetMessage(receiveMessage, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    }
    
Exit:
    if (FAILED(hr))
    {
        // Print out the error
        PrintError(hr, error);
    }
    fflush(
        stdout);
    
    if (sendChannel != NULL)
    {
        // Close the channel
        WsCloseChannel(sendChannel, NULL, error);
    }
    if (sendChannel != NULL)
    {
        WsFreeChannel(sendChannel);
    }
    
    if (receiveChannel != NULL)
    {
        // Close the channel
        WsCloseChannel(receiveChannel, NULL, error);
    }
    if (receiveChannel != NULL)
    {
        WsFreeChannel(receiveChannel);
    }
    
    if (listener != NULL)
    {
        // Close the listener if it was opened
        WsCloseListener(listener, NULL, error);
    }
    if (listener != NULL)
    {
        WsFreeListener(listener);
    }
    
    if (receiveMessage != NULL)
    {
        WsFreeMessage(receiveMessage);
    }
    if (sendMessage != NULL)
    {
        WsFreeMessage(sendMessage);
    }
    if (error != NULL)
    {
        WsFreeError(error);
    }
    if (FAILED(hr))
    {
        // Notify that thread is ready
        threadInfo->hr = hr;
        SetEvent(relayReadyEvent);
    }
    return 1;
}
// Messages arrive on this thread from the relay thread
DWORD WINAPI ReceiverThread(void* parameter)
{
    HRESULT hr;
    THREADINFO* threadInfo = (THREADINFO*) parameter;
    HANDLE receiverReadyEvent = threadInfo->event;
    WS_ERROR* error = NULL;
    WS_CHANNEL* receiveChannel = NULL;
    WS_MESSAGE* receiveMessage = NULL;
    WS_LISTENER* listener = NULL;
    WS_HEAP* heap = NULL;
    
    // Create an error object for storing rich error information
    hr = WsCreateError(
        NULL, 
        0, 
        &error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a heap to store deserialized data
    hr = WsCreateHeap(
        /*maxSize*/ 2048, 
        /*trimSize*/ 512, 
        NULL, 
        0, 
        &heap, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a listener
    hr = WsCreateListener(WS_CHANNEL_TYPE_DUPLEX_SESSION, WS_TCP_CHANNEL_BINDING, NULL, 0, NULL, &listener, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Open listener using TCP duplex session
    hr = WsOpenListener(listener, &serviceUrl, NULL, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a channel
    hr = WsCreateChannelForListener(listener, NULL, 0, &receiveChannel, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Notify that thread is ready
    threadInfo->hr = NOERROR;
    SetEvent(receiverReadyEvent);
    
    // Accept a channel from the client
    hr = WsAcceptChannel(listener, receiveChannel, NULL, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    hr = WsCreateMessageForChannel(
        receiveChannel,
        NULL, 
        0, 
        &receiveMessage, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Receive all messages
    for (;;)
    {
        // Receive start of message (headers)
        hr = WsReadMessageStart(receiveChannel, receiveMessage, NULL, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        if (hr == WS_S_END)
        {
            // No more messages on this channel
            break;
        }
    
        // Get action value
        WS_XML_STRING receivedAction;
        hr = WsGetHeader(
            receiveMessage, 
            WS_ACTION_HEADER,
            WS_XML_STRING_TYPE,
            WS_READ_REQUIRED_VALUE, 
            NULL, 
            &receivedAction, 
            sizeof(receivedAction), 
            error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Make sure action is what we expect
        if (WsXmlStringEquals(&receivedAction, PurchaseOrder_wsdl.messages.PurchaseOrder.action, error) != S_OK)
        {
            hr = WS_E_ENDPOINT_ACTION_NOT_SUPPORTED;
            goto Exit;
        }
    
        // Read the body of the message as a purchase order
        _PurchaseOrderType* purchaseOrder;
        hr = WsReadBody(receiveMessage, &PurchaseOrder_wsdl.globalElements.PurchaseOrderType,
            WS_READ_REQUIRED_POINTER, heap, &purchaseOrder, sizeof(purchaseOrder), error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Print out purchase order
        wprintf(L"%ld, %s\n", 
            purchaseOrder->quantity, 
            purchaseOrder->productName);
    
        // Receive message end
        hr = WsReadMessageEnd(receiveChannel, receiveMessage, NULL, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Reset message so we can use it again
        hr = WsResetMessage(receiveMessage, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Reset heap
        hr = WsResetHeap(heap, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    }
    
Exit:
    if (FAILED(hr))
    {
        // Print out the error
        PrintError(hr, error);
    }
    fflush(
        stdout);
    
    if (receiveChannel != NULL)
    {
        // Close the channel
        WsCloseChannel(receiveChannel, NULL, error);
    }
    if (receiveChannel != NULL)
    {
        WsFreeChannel(receiveChannel);
    }
    
    if (listener != NULL)
    {
        // Close the listener if it was opened
        WsCloseListener(listener, NULL, error);
    }
    if (listener != NULL)
    {
        WsFreeListener(listener);
    }
    
    if (receiveMessage != NULL)
    {
        WsFreeMessage(receiveMessage);
    }
    if (error != NULL)
    {
        WsFreeError(error);
    }
    if (heap != NULL)
    {
        WsFreeHeap(heap);
    }
    if (FAILED(hr))
    {
        // Notify that thread is ready
        SetEvent(receiverReadyEvent);
        threadInfo->hr = hr;
    }
    return 1;
}
// Main entry point
int __cdecl wmain()
{
    
    HRESULT hr = S_OK;
    WS_ERROR* error = NULL;
    WS_LISTENER* listener = NULL;
    WS_CHANNEL* channel = NULL;
    WS_MESSAGE* requestMessage = NULL;
    WS_MESSAGE* replyMessage = NULL;
    WS_HEAP* heap = NULL;
    WCHAR* certSubjectName = NULL;
    PCCERT_CONTEXT certContext = NULL;
    
    BOOL requireClientCert = TRUE;
    WS_SECURITY_BINDING_PROPERTY requireClientCertProperty = {};
    requireClientCertProperty.id = WS_SECURITY_BINDING_PROPERTY_REQUIRE_SSL_CLIENT_CERT;
    requireClientCertProperty.value = &requireClientCert;
    requireClientCertProperty.valueSize = sizeof(requireClientCert);
    
    // declare and initialize an SSL transport security binding
    WS_SSL_TRANSPORT_SECURITY_BINDING sslBinding = {}; // zero out the struct
    sslBinding.binding.bindingType = WS_SSL_TRANSPORT_SECURITY_BINDING_TYPE; // set the binding type
    sslBinding.binding.properties = &requireClientCertProperty;
    sslBinding.binding.propertyCount = 1;
    // NOTE: At the server, the SSL certificate for the listen URI must be
    // registered with http.sys using a tool such as httpcfg.exe.
    
    // declare and initialize the array of all security bindings
    WS_SECURITY_BINDING* securityBindings[1] = { &sslBinding.binding };
    
    // declare and initialize the security description
    WS_SECURITY_DESCRIPTION securityDescription = {}; // zero out the struct
    securityDescription.securityBindings = securityBindings;
    securityDescription.securityBindingCount = WsCountOf(securityBindings);
    
    static const WS_STRING uri = WS_STRING_VALUE(L"https://localhost:8443/example");
    
    // Create an error object for storing rich error information
    hr = WsCreateError(
        NULL, 
        0, 
        &error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a heap to store deserialized data
    hr = WsCreateHeap(
        /*maxSize*/ 2048, 
        /*trimSize*/ 512, 
        NULL, 
        0, 
        &heap, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Create a listener
    hr = WsCreateListener(
        WS_CHANNEL_TYPE_REPLY, 
        WS_HTTP_CHANNEL_BINDING, 
        NULL, 0, 
        &securityDescription, 
        &listener, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    // Open listener 
    hr = WsOpenListener(listener, &uri, NULL, error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    hr = WsCreateChannelForListener(
        listener, 
        NULL, 
        0, 
        &channel, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    hr = WsCreateMessageForChannel(
        channel,
        NULL, 
        0, 
        &requestMessage, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    hr = WsCreateMessageForChannel(
        channel,
        NULL, 
        0, 
        &replyMessage, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }
    
    
    // Receive requests / send replies
    for (int i = 0; i < 300; i++)
    {
        // Accept a request from the client
        hr = WsAcceptChannel(listener, channel, NULL, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Set up the descriptions of the expected messages. We expect either a purchase order
        // or a request for order status.
        const WS_MESSAGE_DESCRIPTION* requestMessageDescriptions[] =
        {
            &PurchaseOrder_wsdl.messages.PurchaseOrder,   // contains a _PurchaseOrderType in the body
            &PurchaseOrder_wsdl.messages.GetOrderStatus,  // contains a GetOrderStatus in the body
        };
        
        // Receive the message and deserialize the element of the body into the appropriate
        // structure, based on the message descriptions.  The value of the body will be
        // allocated in the specified WS_HEAP, and are valid until WsResetHeap is called.
        void* requestBodyPointer;
        ULONG indexOfMatchedMessageDescription;
        
        hr = WsReceiveMessage(channel, requestMessage, requestMessageDescriptions, WsCountOf(requestMessageDescriptions),
            WS_RECEIVE_REQUIRED_MESSAGE, WS_READ_REQUIRED_POINTER, heap,
            &requestBodyPointer, sizeof(requestBodyPointer), &indexOfMatchedMessageDescription, NULL, error);
        
        // Validate the client certificate.
        if (SUCCEEDED(hr))
        {
            // Another way to do client cert validation is to use
            // WS_MESSAGE_PROPERTY_TRANSPORT_SECURITY_WINDOWS_TOKEN here, assuming http.sys
            // is configured to map the cert into a windows token and was able to map the
            // cert to a user account.
            WS_BYTES encodedCert = {};
            hr = WsGetMessageProperty(
                requestMessage,
                WS_MESSAGE_PROPERTY_ENCODED_CERT,
                &encodedCert,
                sizeof(encodedCert),
                error);
            if (FAILED(hr))
            {
                goto Exit;
            }
        
            _Analysis_assume_(encodedCert.bytes != NULL);
            certContext = CertCreateCertificateContext(
                X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
                encodedCert.bytes,
                encodedCert.length);
        
            if (certContext == NULL)
            {
                hr = HRESULT_FROM_WIN32(GetLastError());
                goto Exit;
            }
        
            ULONG subjectNameLength = CertNameToStr(
                certContext->dwCertEncodingType,
                &certContext->pCertInfo->Subject,
                CERT_SIMPLE_NAME_STR,
                NULL,
                0);
        
            if (subjectNameLength == 0)
            {
                hr = HRESULT_FROM_WIN32(GetLastError());
                goto Exit;
            }
        
            certSubjectName = new WCHAR[subjectNameLength];
            if (certSubjectName == NULL)
            {
                hr = E_OUTOFMEMORY;
                goto Exit;
            }
        
            subjectNameLength = CertNameToStr(
                certContext->dwCertEncodingType,
                &certContext->pCertInfo->Subject,
                CERT_SIMPLE_NAME_STR,
                certSubjectName,
                subjectNameLength);
        
            if (subjectNameLength == 0)
            {
                hr = HRESULT_FROM_WIN32(GetLastError());
                goto Exit;
            }
        
            static const WCHAR expectedCertName[] = L"client.com";
        
            wprintf(L"Expected client cert name: %s\n", expectedCertName);
            wprintf(L"Received client cert name: %s\n", certSubjectName);
        
            if (CompareString(
                    LOCALE_INVARIANT,
                    NORM_IGNORECASE,
                    expectedCertName,
                    (int)::wcslen(expectedCertName),
                    certSubjectName,
                    (int)::wcslen(certSubjectName)) != CSTR_EQUAL)
            {
                hr = WS_E_SECURITY_VERIFICATION_FAILURE;
                goto Exit;
            }
        }
        
        // Process the request, and generate the reply
        const WS_MESSAGE_DESCRIPTION* replyMessageDescription = NULL;
        const void* replyBodyPointer = NULL;
        ULONG replyBodySize = 0;
        _OrderConfirmationType orderConfirmation;
        _GetOrderStatusResponseType getOrderStatusResponse;
        
        if (SUCCEEDED(hr))
        {
            // Get the message description that matched
            const WS_MESSAGE_DESCRIPTION* requestMessageDescription = requestMessageDescriptions[indexOfMatchedMessageDescription];
        
            if (requestMessageDescription == &PurchaseOrder_wsdl.messages.PurchaseOrder)
            {
                // The message was a purchase order.  Get the pointer to the deserialized value.
                _PurchaseOrderType* purchaseOrder = (_PurchaseOrderType*)requestBodyPointer;
        
                // Print out purchase order contents
                wprintf(L"%d, %s\n",
                    purchaseOrder->quantity,
                    purchaseOrder->productName);
        
                // Initialize order confirmation data
                orderConfirmation.expectedShipDate = L"1/1/2006";
                orderConfirmation.orderID = 123;
        
                // Setup up reply message
                replyMessageDescription = &PurchaseOrder_wsdl.messages.OrderConfirmation;
                replyBodyPointer = &orderConfirmation;
                replyBodySize = sizeof(orderConfirmation);
            }
            else if (requestMessageDescription == &PurchaseOrder_wsdl.messages.GetOrderStatus)
            {
                // The message was a order status request.  Get the pointer to the deserialized value.
                _GetOrderStatusType* getOrderStatus = (_GetOrderStatusType*)requestBodyPointer;
        
                // Generate a fault if we don't recognize the order ID
                if (getOrderStatus->orderID != 123)
                {
                    // Fill out details about the fault
                    _OrderNotFoundFaultType orderNotFound;
                    orderNotFound.orderID = getOrderStatus->orderID;
                    
                    static const WS_XML_STRING _faultDetailName = WS_XML_STRING_VALUE("OrderNotFound");
                    static const WS_XML_STRING _faultDetailNs = WS_XML_STRING_VALUE("http://example.com");
                    static const WS_XML_STRING _faultAction = WS_XML_STRING_VALUE("http://example.com/fault");
                    static const WS_ELEMENT_DESCRIPTION _faultElementDescription = 
                    { 
                        (WS_XML_STRING*)&_faultDetailName, 
                        (WS_XML_STRING*)&_faultDetailNs, 
                        WS_UINT32_TYPE, 
                        NULL 
                    };
                    static const WS_FAULT_DETAIL_DESCRIPTION orderNotFoundFaultTypeDescription = 
                    { 
                        (WS_XML_STRING*)&_faultAction, 
                        (WS_ELEMENT_DESCRIPTION*)&_faultElementDescription 
                    };
                    
                    // Set fault detail information in the error object
                    hr = WsSetFaultErrorDetail(
                        error,
                        &orderNotFoundFaultTypeDescription,
                        WS_WRITE_REQUIRED_VALUE,
                        &orderNotFound,
                        sizeof(orderNotFound));
                    
                    if (FAILED(hr))
                    {
                        goto Exit;
                    }
                    
                    // Add an error string to the error object.  This string will
                    // be included in the fault that is sent.
                    static const WS_STRING errorMessage = WS_STRING_VALUE(L"Invalid order ID");
                    hr = WsAddErrorString(error, &errorMessage);
                    
                    if (FAILED(hr))
                    {
                        goto Exit;
                    }
        
                    // Use a failure code to indicate that a fault should be sent
                    hr = E_FAIL;
                }
                else
                {
                    // Initialize the order status response
                    getOrderStatusResponse.orderID = getOrderStatus->orderID;
                    getOrderStatusResponse.status = L"Pending";
        
                    // Specify which message description to use for reply
                    replyMessageDescription = &PurchaseOrder_wsdl.messages.GetOrderStatusResponse;
                    replyBodyPointer = &getOrderStatusResponse;
                    replyBodySize = sizeof(getOrderStatusResponse);
                }
            }
        }
        
        // If there was an error receiving the message
        if (FAILED(hr))
        {
            // Send a fault in the body of the reply message.  The information
            // accumulated in the error object is used to populate the fault.
            // The error code is not transmitted but instead is used to
            // generate an error string if no error strings are present in the
            // error object.
            hr = WsSendFaultMessageForError(
                channel,
                replyMessage,
                error,
                hr,
                WS_FULL_FAULT_DISCLOSURE,
                requestMessage,
                NULL,
                error);
        
            if (FAILED(hr))
            {
                goto Exit;
            }
        
            // Reset the error so it can be used again
            hr = WsResetError(error);
            if (FAILED(hr))
            {
                goto Exit;
            }
        
            // Reset the reply message so it can be used again
            hr = WsResetMessage(replyMessage, error);
            if (FAILED(hr))
            {
                goto Exit;
            }
        }
        else
        {
            // Send a reply message
            hr = WsSendReplyMessage(
                channel,
                replyMessage,
                replyMessageDescription,
                WS_WRITE_REQUIRED_VALUE,
                replyBodyPointer,
                replyBodySize,
                requestMessage,
                NULL,
                error);
        
            if (FAILED(hr))
            {
                goto Exit;
            }
        
            // Reset the reply message so it can be used again
            hr = WsResetMessage(replyMessage, error);
            if (FAILED(hr))
            {
                goto Exit;
            }
        }
        
        // Reset the request message so it can be used again
        hr = WsResetMessage(requestMessage, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
        
        // Reset the heap, which will free any allocations made on it
        hr = WsResetHeap(heap, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Close the request
        hr = WsCloseChannel(channel, NULL, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Prepare channel for reuse
        hr = WsResetChannel(channel, error);
        if (FAILED(hr))
        {
            goto Exit;
        }
    
        // Delete the certificate name string if needed.
        if (certSubjectName != NULL)
        {
            delete[] certSubjectName;
            certSubjectName = NULL;
        }
    
        // Delete cert context if needed
        if (certContext != NULL)
        {
            CertFreeCertificateContext(certContext);
            certContext = NULL;
        }
    }
    
Exit:
    if (FAILED(hr))
    {
        // Print out the error
        PrintError(hr, error);
    }
    
    if (channel != NULL)
    {
        // Close the channel
        WsCloseChannel(channel, NULL, error);
    }
    if (channel != NULL)
    {
        WsFreeChannel(channel);
    }
    if (requestMessage != NULL)
    {
        WsFreeMessage(requestMessage);
    }
    if (replyMessage != NULL)
    {
        WsFreeMessage(replyMessage);
    }
    if (listener != NULL)
    {
        // Close the listener if it was opened
        WsCloseListener(listener, NULL, error);
    }
    if (listener != NULL)
    {
        WsFreeListener(listener);
    }
    
    
    if (error != NULL)
    {
        WsFreeError(error);
    }
    if (heap != NULL)
    {
        WsFreeHeap(heap);
    }
    
    if (certSubjectName != NULL)
    {
        delete[] certSubjectName;
    }
    
    if (certContext != NULL)
    {
        CertFreeCertificateContext(certContext);
    }
    
    fflush(stdout);
    return SUCCEEDED(hr) ? 0 : -1;
}