/// <summary>
        /// Get output relay status
        /// </summary>
        /// <returns>Returns int array [0..8] with status flags of each realya status. arr[0] is for read status (-1 for error, 1 for good read, 0 for smth else)</returns> 
        public int[] getOutputStatus()
        {
            tl.LogMessage("getOutputStatus", "Enter");

            // get the ip9212 settings from the profile
            //readSettings();

            //return data
            int[] ipdata = new int[1] { 0 };

            if (string.IsNullOrEmpty(ip_addr))
            {
                ipdata[0] = -1;
                tl.LogMessage("getOutputStatus", "ERROR (ip_addr wasn't set)!");
                // report a problem with the port name
                ASCOM_ERROR_MESSAGE = "getOutputStatus(): no IP address was specified";
                throw new ASCOM.ValueNotSetException(ASCOM_ERROR_MESSAGE);
                //return input_state_arr;

            }
            string siteipURL;
            siteipURL = "http://" + ip_login + ":" + ip_pass + "@" + ip_addr + ":" + ip_port + "/set.cmd?cmd=getpower";
            // new style
            siteipURL = "http://" + ip_addr + ":" + ip_port + "/Set.cmd?user="******"+pass="******"CMD=getpower";

            //FOR DEBUGGING
            if (debugFlag)
            {
                siteipURL = "http://localhost/ip9212/getpower.php";
            }
            tl.LogMessage("getOutputStatus", "Download url:" + siteipURL);

            // Send http query
            tl.LogMessage("Semaphore", "waitone");
            IP9212Semaphore.WaitOne(); // lock working with IP9212

            string s = "";
            MyWebClient client = new MyWebClient();
            try
            {
                Stream data = client.OpenRead(siteipURL);
                StreamReader reader = new StreamReader(data);
                s = reader.ReadToEnd();
                data.Close();
                reader.Close();

                tl.LogMessage("getOutputStatus", "Download str:" + s);

                tl.LogMessage("Semaphore", "Release");
                IP9212Semaphore.Release();//unlock ip9212 device for others
                //wait
                //Thread.Sleep(1000);

            }
            catch (WebException e)
            {
                tl.LogMessage("Semaphore", "Release");
                IP9212Semaphore.Release();//unlock ip9212 device for others
                ipdata[0] = -1;
                tl.LogMessage("getOutputStatus", "Error:" + e.Message);
                ASCOM_ERROR_MESSAGE = "getInputStatus(): Couldn't reach network server";
                //throw new ASCOM.NotConnectedException(ASCOM_ERROR_MESSAGE);
                Trace("> IP9212_harware.getOutputStatus(): exit by web error");
                tl.LogMessage("getOutputStatus", "Exit by web error");
                return ipdata;
            }

            // Parse data
            try
            {
                string[] stringSeparators = new string[] { "P6" };
                string[] iprawdata_arr = s.Split(stringSeparators, StringSplitOptions.None);

                Array.Resize(ref ipdata, iprawdata_arr.Length);

                //Parse an array
                for (var i = 1; i < iprawdata_arr.Length; i++)
                {
                    //Убираем запятую
                    if (iprawdata_arr[i].Length > 3)
                    {
                        iprawdata_arr[i] = iprawdata_arr[i].Substring(0, 3);
                    }
                    //Console.WriteLine(iprawdata_arr[i]);

                    //Разбиваем на пары "номер порта"="значение"
                    char[] delimiterChars = { '=' };
                    string[] data_arr = iprawdata_arr[i].Split(delimiterChars);
                    //st = st + " |" + i + ' ' + data_arr[1];
                    if (data_arr.Length > 1)
                    {
                        ipdata[i] = Convert.ToInt16(data_arr[1]);
                        Trace(ipdata[i]);
                    }
                    else
                    {
                        ipdata[i] = -1;
                    }
                }
                ipdata[0] = 1;
                tl.LogMessage("getOutputStatus", "Data was read");
            }
            catch
            {
                ipdata[0] = -1;
                tl.LogMessage("getOutputStatus", "ERROR (Exception)!");
                tl.LogMessage("getOutputStatus", "exit by parse error");
                return ipdata;
            }
            return ipdata;
        }
        /// <summary>
        /// Check the availability of IP server by starting async read from input sensors. Result handeled to checkLink_DownloadCompleted()
        /// </summary>
        /// <returns>Nothing</returns> 
        public void checkLink_async()
        {
            tl.LogMessage("CheckLink_async", "enter");

            //Check - address was specified?
            if (string.IsNullOrEmpty(ip_addr))
            {
                hardware_connected_flag = false;
                tl.LogMessage("CheckLink_async", "ERROR (ip_addr wasn't set)!");
                // report a problem with the port name
                //throw new ASCOM.DriverException("checkLink_async error");
                return;
            }

            string siteipURL;
            siteipURL = "http://" + ip_login + ":" + ip_pass + "@" + ip_addr + ":" + ip_port + "/set.cmd?cmd=getio";
            // new style
            siteipURL = "http://" + ip_addr + ":" + ip_port + "/Set.cmd?user="******"+pass="******"CMD=getio";
            //FOR DEBUGGING
            if (debugFlag)
            {
                siteipURL = "http://localhost/ip9212/getio.php";
            }
            Uri uri_siteipURL = new Uri(siteipURL);
            tl.LogMessage("CheckLink_async", "download url:" + siteipURL);

            // Send http query
            MyWebClient client = new MyWebClient();
            try
            {
                tl.LogMessage("Semaphore", "WaitOne");
                IP9212Semaphore.WaitOne(); // lock working with IP9212

                client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(checkLink_DownloadCompleted);

                client.DownloadDataAsync(uri_siteipURL);

                tl.LogMessage("CheckLink_async", "http request was sent");
            }
            catch (WebException e)
            {
                tl.LogMessage("Semaphore", "Release");
                IP9212Semaphore.Release();//unlock ip9212 device for others
                hardware_connected_flag = false;
                tl.LogMessage("CheckLink_async", "error:" + e.Message);
                //throw new ASCOM.NotConnectedException("Couldn't reach network server");
                tl.LogMessage("CheckLink_async", "exit on web error");
            }
        }
        /// <summary>
        /// Check the availability of IP server by straight read (NON ASYNC manner)
        /// </summary>  
        /// <returns>Aviability of IP server </returns> 
        public bool checkLink_forced()
        {
            tl.LogMessage("checkLink_forced", "Enter");

            //Check - address was specified?
            if (string.IsNullOrEmpty(ip_addr))
            {
                hardware_connected_flag = false;
                tl.LogMessage("checkLink_forced", "ERROR (ip_addr wasn't set)!");
                // report a problem with the port name
                //throw new ASCOM.DriverException("checkLink_async error");
                return hardware_connected_flag;
            }

            string siteipURL;
            siteipURL = "http://" + ip_login + ":" + ip_pass + "@" + ip_addr + ":" + ip_port + "/set.cmd?cmd=getio";
            // new style
            siteipURL = "http://" + ip_addr + ":" + ip_port + "/Set.cmd?user="******"+pass="******"CMD=getio";

            //FOR DEBUGGING
            if (debugFlag)
            {
                siteipURL = "http://localhost/ip9212/getio.php";
            }

            Uri uri_siteipURL = new Uri(siteipURL);
            tl.LogMessage("checkLink_forced", "Download url:" + siteipURL);

            // Send http query
            tl.LogMessage("Semaphore", "waitone");
            IP9212Semaphore.WaitOne(); // lock working with IP9212

            string s = "";
            MyWebClient client = new MyWebClient();
            try
            {
                Stream data = client.OpenRead(uri_siteipURL);
                StreamReader reader = new StreamReader(data);
                s = reader.ReadToEnd();
                data.Close();
                reader.Close();

                tl.LogMessage("checkLink_forced", "Download str:" + s);
                //wait
                //Thread.Sleep(1000);

                tl.LogMessage("Semaphore", "Release");
                int ns = IP9212Semaphore.Release();//unlock ip9212 device for others
                tl.LogMessage("Semaphore", "left count " + ns);

                if (s.IndexOf("P5") >= 0)
                {
                    hardware_connected_flag = true;
                    tl.LogMessage("checkLink_forced", "Downloaded data is ok");
                }
                else
                {
                    hardware_connected_flag = false;
                    tl.LogMessage("checkLink_forced", "Downloaded data error - string not found");
                }
            }
            catch (WebException e)
            {
                tl.LogMessage("Semaphore", "Release");
                IP9212Semaphore.Release();//unlock ip9212 device for others
                hardware_connected_flag = false;
                tl.LogMessage("checkLink_forced", "Error" + e.Message);
                //throw new ASCOM.NotConnectedException("Couldn't reach network server");
                tl.LogMessage("checkLink_forced", "Exit by web error");
            }
            tl.LogMessage("checkLink_forced", "Exit, ret value " + hardware_connected_flag.ToString());
            return hardware_connected_flag;
        }
        /// <summary>
        /// Chage output relay state
        /// </summary>
        /// <param name="PortNumber">Relay port number, int [1..9]</param>
        /// <param name="PortValue">Port value [0,1]</param>
        /// <returns>Returns true in case of success</returns> 
        public bool setOutputStatus(int PortNumber, int PortValue)
        {
            tl.LogMessage("setOutputStatus", "Enter (" + PortNumber + "," + PortValue + ")");

            // get the ip9212 settings from the profile
            //readSettings();

            //return data
            bool ret = false;

            if (string.IsNullOrEmpty(ip_addr))
            {
                tl.LogMessage("setOutputStatus", "ERROR (ip_addr wasn't set)!");
                // report a problem with the port name
                ASCOM_ERROR_MESSAGE = "setOutputStatus(): no IP address was specified";
                throw new ASCOM.ValueNotSetException(ASCOM_ERROR_MESSAGE);
                //return ret;
            }
            string siteipURL = "http://" + ip_login + ":" + ip_pass + "@" + ip_addr + ":" + ip_port + "/set.cmd?cmd=setpower+P6" + PortNumber + "=" + PortValue;
            // new style
            siteipURL = "http://" + ip_addr + ":" + ip_port + "/Set.cmd?user="******"+pass="******"CMD=setpower+P6" + PortNumber + "=" + PortValue;
            //FOR DEBUGGING
            if (debugFlag)
            {
                siteipURL = "http://localhost/ip9212/set.php?cmd=setpower+P6" + PortNumber + "=" + PortValue;
            }
            tl.LogMessage("setOutputStatus", "Download url:" + siteipURL);

            // Send http query
            tl.LogMessage("Semaphore", "waitone");
            IP9212Semaphore.WaitOne(); // lock working with IP9212
            string s = "";
            MyWebClient client = new MyWebClient();
            try
            {
                Stream data = client.OpenRead(siteipURL);
                StreamReader reader = new StreamReader(data);
                s = reader.ReadToEnd();
                data.Close();
                reader.Close();

                tl.LogMessage("setOutputStatus", "Download str:" + s);

                //wait
                //Thread.Sleep(1000);
                tl.LogMessage("Semaphore", "Release");
                IP9212Semaphore.Release();//unlock ip9212 device for others

                ret = true;
            }
            catch (WebException e)
            {
                tl.LogMessage("Semaphore", "Release");
                IP9212Semaphore.Release();//unlock ip9212 device for others
                ret = false;

                tl.LogMessage("setOutputStatus", "Error:" + e.Message);
                ASCOM_ERROR_MESSAGE = "setOutputStatus(" + PortNumber + "," + PortValue + "): Couldn't reach network server";
                //throw new ASCOM.NotConnectedException(ASCOM_ERROR_MESSAGE);
                tl.LogMessage("setOutputStatus", "Exit by web error");
                return ret;
                // report a problem with the port name (never get there)
            }
            // Parse data
            // not implemented yet

            return ret;
        }