/// <summary>
        /// Method runs a loop waiting for Hello events. When on is received method probes, resolves
        /// and Get's device service information and calls 1way, 2way methods and subscribes to SimpleEvent
        /// and IntegerEvent and waits for them to fire. When they do Unsubscribe is called on the events
        /// and the receive hello's flag is reset.
        /// </summary>
        public void Run()
        {
            // Create Event handlers
            m_simpleServiceClient.ByeEvent             += new ByeEventHandler(m_simpleControl_ByeEvent);
            m_simpleServiceClient.HelloEvent           += new HelloEventHandler(m_simpleControl_HelloEvent);
            m_simpleServiceClient.SubscriptionEndEvent += new SubscriptionEndEventHandler(m_simpleControl_SubscriptionEndEvent);
            bool firstPass = true;

            bool twoWayAttach = false;

            DpwsServiceTypes typeProbes = new DpwsServiceTypes();

            typeProbes.Add(new DpwsServiceType("SimpleDeviceType", "http://schemas.example.org/SimpleService"));

            // Continuous run loop
            while (true)
            {
                if (firstPass && !m_inDiscovery)
                {
                    DpwsServiceDescriptions descs = m_simpleServiceClient.DiscoveryClient.Probe(typeProbes, 3, 1000);

                    for (int i = 0; i < descs.Count; i++)
                    {
                        DpwsServiceDescription desc = descs[i];

                        if (desc.XAddrs != null && desc.XAddrs.Length > 0)
                        {
                            CheckConnection(desc.ServiceTypes, desc.Endpoint.Address.AbsoluteUri);
                        }
                    }
                }

                // If hello was received and a SimpleService device was found. SeletedService will not be null.
                // Process until Bye is received.
                if (m_deviceSelected.WaitOne(5000, false))
                {
                    // If this is the first time through the loop for this device subscribe to events
                    if (firstPass)
                    {
                        // Test service host call
                        System.Ext.Console.Write("Testing Host service...");
                        DpwsSubscribeRequest subReq;
                        subReq = new DpwsSubscribeRequest(m_eventingServiceClient.EventSources["SimpleEvent"], m_eventingServiceClient.EndpointAddress, m_eventingServiceClient.TransportAddress, "PT1H", null);
                        m_simpleEventSubscription = m_eventingServiceClient.EventingClient.Subscribe(subReq);
                        subReq = new DpwsSubscribeRequest(m_eventingServiceClient.EventSources["IntegerEvent"], m_eventingServiceClient.EndpointAddress, m_eventingServiceClient.TransportAddress, "PT1H", null);
                        m_integerEventSubscription = m_eventingServiceClient.EventingClient.Subscribe(subReq);

                        firstPass = false;
                    }

                    // Make 1Way and 2Way service calls
                    if (m_deviceSelected.WaitOne(0, false))
                    {
                        PrintMetadataInfo();

                        System.Ext.Console.Write("");
                        System.Ext.Console.Write(">>>>>>>>>>>>> Sending 1way(10) request to: " + m_selectedService.ThisDevice.FriendlyName);
                        try
                        {
                            m_simpleServiceClient.OneWay(new OneWayRequest());
                        }
                        catch (Exception e)
                        {
                            System.Ext.Console.Write("");
                            System.Ext.Console.Write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! OneWay method failed. " + e.Message);
                        }
                    }

                    if (m_deviceSelected.WaitOne(0, false))
                    {
                        int x = System.Math.Abs(m_random.Next()) % 100;
                        int y = System.Math.Abs(m_random.Next()) % 100;

                        System.Ext.Console.Write("");
                        System.Ext.Console.Write(">>>>>>>>>>>>> Sending 2way(" + x.ToString() + ", " + y.ToString() + ") request to: " + m_selectedService.ThisDevice.FriendlyName);
                        try
                        {
                            TwoWayRequest req = new TwoWayRequest();
                            req.X = x;
                            req.Y = y;

                            TwoWayResponse resp = m_simpleServiceClient.TwoWay(req);
                            if (resp.Sum == 0)
                            {
                                System.Ext.Console.Write("");
                                System.Ext.Console.Write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 2way method did not receive a valid response.");
                            }
                            else
                            {
                                System.Ext.Console.Write("");
                                System.Ext.Console.Write("<<<<<<<<<<<<< 2way response returned " + resp.Sum);
                            }
                        }
                        catch (Exception e)
                        {
                            System.Ext.Console.Write("");
                            System.Ext.Console.Write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TwoWay method failed. " + e.Message);
                        }
                    }

                    // Make 1wayattach or a 2wayattach service calls
                    if (m_deviceSelected.WaitOne(0, false))
                    {
                        if (!twoWayAttach)
                        {
                            // create an instance of the help icon test object
                            HelpIcon helpIcon = new HelpIcon();

                            System.Ext.Console.Write("");
                            System.Ext.Console.Write(">>>>>>>>>>>>> Sending 1wayattach request to: " + m_selectedService.ThisDevice.FriendlyName);
                            try
                            {
                                OneWayAttachmentRequest req = new OneWayAttachmentRequest();
                                req.Param = helpIcon.Data.ToArray();
                                m_attachmentServiceClient.OneWayAttachment(req);
                            }
                            catch (Exception e)
                            {
                                System.Ext.Console.Write("");
                                System.Ext.Console.Write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 1wayattach method failed: " + e.Message);
                            }
                        }
                        else
                        {
                            // create an instance of the help icon test object
                            HelpIcon helpIcon = new HelpIcon();

                            System.Ext.Console.Write("");
                            System.Ext.Console.Write(">>>>>>>>>>>>> Sending 2wayattach request to: " + m_selectedService.ThisDevice.FriendlyName);
                            try
                            {
                                TwoWayAttachmentRequest req = new TwoWayAttachmentRequest();
                                req.Param = helpIcon.Data.ToArray();
                                TwoWayAttachmentResponse resp = m_attachmentServiceClient.TwoWayAttachment(req);

                                System.Ext.Console.Write("");
                                System.Ext.Console.Write("<<<<<<<<<<<<< Sending 2wayattach request succeeded");
                            }
                            catch (Exception e)
                            {
                                System.Ext.Console.Write("");
                                System.Ext.Console.Write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TwoWay method failed. " + e.Message);
                            }
                        }

                        twoWayAttach = !twoWayAttach;
                    }
                }
                else
                {
                    firstPass = true;
                }
            }
        }
        // TwoWayAttachmentResp: Construct response message for TwoWayAttachmentRequest
        public WsMessage TwoWayAttachmentResp(WsWsaHeader header)
        {
            // Create xmlWriter object
            MemoryStream soapStream = new MemoryStream();
            XmlWriter    xmlWriter  = XmlWriter.Create(soapStream);

            // Write processing instructions and root element
            xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
            xmlWriter.WriteStartElement("soap", "Envelope", "http://www.w3.org/2003/05/soap-envelope");

            // Write namespaces
            ProtocolVersion10 ver = new ProtocolVersion10();

            xmlWriter.WriteAttributeString("xmlns", "wsa", null, ver.AddressingNamespace);
            xmlWriter.WriteAttributeString("xmlns", "xop", null, WsWellKnownUri.XopNamespaceUri);
            xmlWriter.WriteAttributeString("xmlns", "att", null, "http://schemas.example.org/AttachmentService");

            // Write header
            xmlWriter.WriteStartElement("soap", "Header", null);
            xmlWriter.WriteStartElement("wsa", "To", null);
            xmlWriter.WriteString("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous");
            xmlWriter.WriteEndElement(); // End To
            xmlWriter.WriteStartElement("wsa", "Action", null);
            xmlWriter.WriteString("http://schemas.example.org/AttachmentService/TwoWayAttachmentResponse");
            xmlWriter.WriteEndElement(); // End Action
            xmlWriter.WriteStartElement("wsa", "RelatesTo", null);
            xmlWriter.WriteString(header.MessageID);
            xmlWriter.WriteEndElement(); // End RelatesTo
            xmlWriter.WriteStartElement("wsa", "MessageID", null);
            xmlWriter.WriteString("urn:uuid:" + Guid.NewGuid().ToString());
            xmlWriter.WriteEndElement(); // End To
            xmlWriter.WriteEndElement(); // End Header

            // write body
            xmlWriter.WriteStartElement("soap", "Body", null);
            xmlWriter.WriteStartElement("att", "TwoWayAttachmentResponse", null);
            xmlWriter.WriteStartElement("att", "Param", null);
            xmlWriter.WriteStartElement("xop", "Include", null);
            xmlWriter.WriteAttributeString("href", "cid:0@body");
            xmlWriter.WriteString("");
            xmlWriter.WriteEndElement(); // End Include
            xmlWriter.WriteEndElement(); // End Param
            xmlWriter.WriteEndElement(); // End TwoWayAttachmentResponse
            xmlWriter.WriteEndElement(); // End Body

            xmlWriter.WriteEndElement(); // End Envelope

            // Create return buffer and close writer
            xmlWriter.Flush();
            xmlWriter.Close();

            WsMessage msg = new WsMessage(null, soapStream.ToArray(), WsPrefix.Wse);

            // Clear the hosted service's MTOM body parts collection and build return body parts collection
            // Create and store soap body part (As per spec soap envelope must be first body part)
            WsMtomBodyPart bodyPart = new WsMtomBodyPart();

            // Create and store attachment body part
            bodyPart = new WsMtomBodyPart();
            HelpIcon testIcon = new HelpIcon();

            bodyPart.Content   = testIcon.Data;
            bodyPart.ContentID = "<0@body>";
            bodyPart.ContentTransferEncoding = "binary";
            bodyPart.ContentType             = "application/octet-stream";
            msg.BodyParts.Add(bodyPart);

            // Set the response type so the HTTP processor knows we are sending MTOM
            //MessageType = WsMessageType.Mtom;

            // We are returning the actual response in the hosted service's body parts, so return null
            return(msg);
        }
        // TwoWayAttachmentResp: Construct response message for TwoWayAttachmentRequest
        public WsMessage TwoWayAttachmentResp(WsWsaHeader header)
        {
            // Create xmlWriter object
            MemoryStream soapStream = new MemoryStream();
            XmlWriter xmlWriter = XmlWriter.Create(soapStream);

            // Write processing instructions and root element
            xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
            xmlWriter.WriteStartElement("soap", "Envelope", "http://www.w3.org/2003/05/soap-envelope");

            // Write namespaces
            ProtocolVersion10 ver = new ProtocolVersion10();
            xmlWriter.WriteAttributeString("xmlns", "wsa", null, ver.AddressingNamespace);
            xmlWriter.WriteAttributeString("xmlns", "xop", null, WsWellKnownUri.XopNamespaceUri);
            xmlWriter.WriteAttributeString("xmlns", "att", null, "http://schemas.example.org/AttachmentService");

            // Write header
            xmlWriter.WriteStartElement("soap", "Header", null);
            xmlWriter.WriteStartElement("wsa", "To", null);
            xmlWriter.WriteString("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous");
            xmlWriter.WriteEndElement(); // End To
            xmlWriter.WriteStartElement("wsa", "Action", null);
            xmlWriter.WriteString("http://schemas.example.org/AttachmentService/TwoWayAttachmentResponse");
            xmlWriter.WriteEndElement(); // End Action
            xmlWriter.WriteStartElement("wsa", "RelatesTo", null);
            xmlWriter.WriteString(header.MessageID);
            xmlWriter.WriteEndElement(); // End RelatesTo
            xmlWriter.WriteStartElement("wsa", "MessageID", null);
            xmlWriter.WriteString("urn:uuid:" + Guid.NewGuid().ToString());
            xmlWriter.WriteEndElement(); // End To
            xmlWriter.WriteEndElement(); // End Header

            // write body
            xmlWriter.WriteStartElement("soap", "Body", null);
            xmlWriter.WriteStartElement("att", "TwoWayAttachmentResponse", null);
            xmlWriter.WriteStartElement("att", "Param", null);
            xmlWriter.WriteStartElement("xop", "Include", null);
            xmlWriter.WriteAttributeString("href", "cid:0@body");
            xmlWriter.WriteString("");
            xmlWriter.WriteEndElement(); // End Include
            xmlWriter.WriteEndElement(); // End Param
            xmlWriter.WriteEndElement(); // End TwoWayAttachmentResponse
            xmlWriter.WriteEndElement(); // End Body

            xmlWriter.WriteEndElement(); // End Envelope

            // Create return buffer and close writer
            xmlWriter.Flush();
            xmlWriter.Close();

            WsMessage msg = new WsMessage(null, soapStream.ToArray(), WsPrefix.Wse);

            // Clear the hosted service's MTOM body parts collection and build return body parts collection
            // Create and store soap body part (As per spec soap envelope must be first body part)
            WsMtomBodyPart bodyPart = new WsMtomBodyPart();

            // Create and store attachment body part
            bodyPart = new WsMtomBodyPart();
            HelpIcon testIcon = new HelpIcon();
            bodyPart.Content = testIcon.Data;
            bodyPart.ContentID = "<0@body>";
            bodyPart.ContentTransferEncoding = "binary";
            bodyPart.ContentType = "application/octet-stream";
            msg.BodyParts.Add(bodyPart);

            // Set the response type so the HTTP processor knows we are sending MTOM
            //MessageType = WsMessageType.Mtom;

            // We are returning the actual response in the hosted service's body parts, so return null
            return msg;
        }