/// <summary>
        /// Called when a response is received against a sent request
        /// </summary>
        /// <param name="coapResp">The CoAPResponse object</param>
        private void OnCoAPResponseReceived(CoAPResponse coapResp)
        {
            string tokenRx = (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";

            if (tokenRx == __Token)
            {
                if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                {
                    ArrayList options = coapResp.Options.GetOptions((ushort)CoAPHeaderOption.CONTENT_FORMAT);
                    if (options.Count > 0)
                    {
                        bool proceed = false;
                        foreach (CoAPHeaderOption o in options)
                        {
                            CoAPContentFormatOption ccformat = new CoAPContentFormatOption(AbstractByteUtils.ToUInt16(o.Value));
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_LINK_FORMAT)
                            {
                                proceed = true;
                            }
                        }
                        if (proceed)
                        {
                            string discovery = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
                            Console.WriteLine("Discovery " + __coapClient.EndPoint.ToString() + " = " + discovery);
                            __DiscoveryResult = discovery;
                        }
                    }
                }
                else
                {
                    //Will come here if an error occurred..
                }
            }
            __Done.Set();
        }
        /// <summary>
        /// Called when a response is received against a sent request
        /// </summary>
        /// <param name="coapResp">The CoAPResponse object</param>
        private void OnCoAPResponseReceived(CoAPResponse coapResp)
        {
            string tokenRx = (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";

            FileLogger.Write("Received response from server - token = " + tokenRx);
            FileLogger.Write(coapResp.ToString());
            if (tokenRx == __Token)
            {
                if (BreakIfError(coapResp.Code.Value))
                {
                    return;
                }

                if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                {
                    ArrayList options = coapResp.Options.GetOptions((ushort)CoAPHeaderOption.CONTENT_FORMAT);
                    if (options.Count > 0)
                    {
                        CoAPContentFormatOption ccformat = new CoAPContentFormatOption();
                        bool proceed = false;
                        foreach (CoAPHeaderOption o in options)
                        {
                            ccformat = new CoAPContentFormatOption(AbstractByteUtils.ToUInt16(o.Value));
                            if (ccformat.IsValid())
                            {
                                proceed = true;
                                break;
                            }
                        }
                        if (proceed)
                        {
                            if (ccformat.Value == CoAPContentFormatOption.TEXT_PLAIN)
                            {
                                string result = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
                                __PingResult = result;
                            }
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_OCTET_STREAM)
                            {
                                string result = HdkUtils.BytesToHexView(coapResp.Payload.Value);
                                __PingResult = result;
                            }
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_JSON)
                            {
                                string result = HdkUtils.BytesToHexView(coapResp.Payload.Value);
                                __PingResult = result;
                            }

                            __Response = coapResp;
                            __Done.Set();
                        }
                    }
                }
                else
                {
                    //Will come here if an error occurred..
                }
            }
        }
        ///// <summary>
        ///// Called when a request is received...
        ///// </summary>
        ///// <param name="coapReq">The CoAPRequest object</param>
        //private void OnCoAPRequestReceived(CoAPRequest coapReq)
        //{
        //    Console.WriteLine(coapReq.QueryString);
        //}

        /// <summary>
        /// Called when a response is received against a sent request
        /// </summary>
        /// <param name="coapResp">The CoAPResponse object</param>
        private void OnCoAPResponseReceived(CoAPResponse coapResp)
        {
            string tokenRx = (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";

            if (tokenRx == __Token)
            {
                if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                {
                    ArrayList options = coapResp.Options.GetOptions((ushort)CoAPHeaderOption.CONTENT_FORMAT);
                    if (options.Count > 0)
                    {
                        CoAPContentFormatOption ccformat = new CoAPContentFormatOption();
                        bool proceed = false;
                        foreach (CoAPHeaderOption o in options)
                        {
                            ccformat = new CoAPContentFormatOption(AbstractByteUtils.ToUInt16(o.Value));
                            if (ccformat.IsValid())
                            {
                                proceed = true;
                                break;
                            }
                        }
                        if (proceed)
                        {
                            if (ccformat.Value == CoAPContentFormatOption.TEXT_PLAIN)
                            {
                                string result = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
                                Console.WriteLine("Get on Olimex " + __coapClient.EndPoint.ToString() + " = " + result);
                                __GetResult = result;
                            }
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_OCTET_STREAM)
                            {
                                string result = SSNUtils.Conversion.BytesToHexView(coapResp.Payload.Value);
                                Console.WriteLine("Get on Olimex " + __coapClient.EndPoint.ToString() + " = " + result);
                                __GetResult = result;
                            }
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_JSON)
                            {
                                string result = SSNUtils.Conversion.BytesToHexView(coapResp.Payload.Value);
                                Console.WriteLine("Get on Olimex " + __coapClient.EndPoint.ToString() + " = " + result);
                                __GetResult = result;
                            }

                            __Response = coapResp;
                        }
                    }
                }
                else
                {
                    //Will come here if an error occurred..
                }
            }
            __Done.Set();
        }
        public void SetGetResult(byte[] payload)
        {
            string discovery = AbstractByteUtils.ByteToStringUTF8(payload);

            Console.WriteLine("Discovery = " + discovery);
            __GetResult       = discovery;
            __InternalPayload = payload;
            CoAPResponse coapResp = new CoAPResponse();

            try
            {
                coapResp.FromByteStream(payload);
                string tokenRx = (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";
                if (tokenRx == __Token)
                {
                    if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                    {
                        ArrayList options = coapResp.Options.GetOptions((ushort)CoAPHeaderOption.CONTENT_FORMAT);
                        if (options.Count > 0)
                        {
                            CoAPContentFormatOption ccformat = new CoAPContentFormatOption();
                            bool proceed = false;
                            foreach (CoAPHeaderOption o in options)
                            {
                                ccformat = new CoAPContentFormatOption(AbstractByteUtils.ToUInt16(o.Value));
                                if (ccformat.IsValid())
                                {
                                    proceed = true;
                                    break;
                                }
                            }
                            if (proceed)
                            {
                                if (ccformat.Value == CoAPContentFormatOption.TEXT_PLAIN)
                                {
                                    string result = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
                                    Console.WriteLine("Get " + " = " + result);
                                    __GetResult = result;
                                }
                                if (ccformat.Value == CoAPContentFormatOption.APPLICATION_OCTET_STREAM)
                                {
                                    string result = HdkUtils.BytesToHexView(coapResp.Payload.Value);
                                    Console.WriteLine("Get " + " = " + result);
                                    __GetResult = result;
                                }
                                if (ccformat.Value == CoAPContentFormatOption.APPLICATION_JSON)
                                {
                                    string result = HdkUtils.BytesToHexView(coapResp.Payload.Value);
                                    Console.WriteLine("Get " + " = " + result);
                                    __GetResult = result;
                                }

                                __Response = coapResp;
                            }
                        }
                    }
                    else
                    {
                        //Will come here if an error occurred..
                    }
                }
            }
            catch
            { }
        }
        /// <summary>
        /// Called when a response is received against a sent request
        /// </summary>
        /// <param name="coapResp">The CoAPResponse object</param>
        private void OnCoAPDiscoveryResponseReceived(CoAPResponse coapResp)
        {
            __TimedOut = false;
            string tokenRx = "";// (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";

            if (coapResp != null)
            {
                if (coapResp.Token != null)
                {
                    tokenRx = AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value);
                }
            }
            try
            {
                FileLogger.Write("Received response from server - token = " + tokenRx);
                FileLogger.Write(coapResp.ToString());
            }
            catch { }

            if (tokenRx == __Token)
            {
                if (BreakIfError(coapResp.Code.Value))
                {
                    this.ErrorResult = coapResp.Code.ToString();
                    return;
                }

                if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                {
                    // Looks like we have the expected response.
                    // Check the content format and figure out how to store the response.
                    ArrayList options = coapResp.Options.GetOptions((ushort)CoAPHeaderOption.CONTENT_FORMAT);
                    if (options.Count > 0)
                    {
                        CoAPContentFormatOption ccformat = new CoAPContentFormatOption();
                        bool proceed = false;
                        foreach (CoAPHeaderOption o in options)
                        {
                            ccformat = new CoAPContentFormatOption(AbstractByteUtils.ToUInt16(o.Value));
                            if (ccformat.IsValid())
                            {
                                proceed = true;
                                break;
                            }
                        }
                        // The header options match what we expected.
                        // Set the discovery result.
                        if (proceed)
                        {
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_LINK_FORMAT)
                            {
                                string discovery = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
                                __DiscoveryResult = discovery;
                            }

                            __Response = coapResp;
                            __Done.Set();
                        }
                    }
                }
                else
                {
                    //Will come here if an error occurred..
                }
            }
        }