public void listen_for_error_and_state()
        {
            string res_sta = "";
            string res_err = "";

            try
            {
                while (true)
                {
                    String timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    String str       = cmd_query_out;
                    //Console.WriteLine("Transmitting for query: " + str);

                    Console.WriteLine(timestamp + " -------------------------------------------------");

                    res_sta = query(cmd_query_out);
                    Console.WriteLine(res_sta);

                    //ID of bit in state bytes
                    int q1b   = 2;
                    int q2b   = 3;
                    int teilb = 7;

                    string rel = res_sta.Substring(5, 2); //Takes last 2 places of response
                    Console.WriteLine(rel);
                    byte r = byte.Parse(rel, System.Globalization.NumberStyles.HexNumber);
                    //Console.WriteLine("Zustandbytes:");
                    //Console.WriteLine(r);

                    bool q1   = (r & (1 << q1b)) != 0;
                    bool q2   = (r & (1 << q2b)) != 0;
                    bool teil = (r & (1 << teilb)) != 0;

                    Console.WriteLine("q1:   " + q1);
                    Console.WriteLine("q2:   " + q2);
                    Console.WriteLine("teil: " + teil);

                    if (q1 && q2 && (teil == false))
                    {
                        Console.WriteLine("KOLBEN NACHFUELLEN");
                        q1 = false;
                        q2 = false;
                    }

                    if (q1 && q2 && teil)
                    {
                        Console.WriteLine("TEIL VERLOREN");
                        q1 = false;
                        q2 = false;
                    }

                    if (q1 == false && q2 && teil == false)
                    {
                        Console.WriteLine("DECKEL NACHFUELLEN");
                    }

                    if (q1 && q2 == false && teil == false)
                    {
                        Console.WriteLine("FEDERN NACHFUELLEN");
                    }



                    Console.Write("... \n");//---------------------------------
                    res_err = query(cmd_query_err);
                    Console.WriteLine(res_err);

                    //char errorstate_c = res[2];
                    bool   robot_state_bool = res_err.Contains('K');
                    string robot_state_str  = "FEHLER";
                    if (robot_state_bool)
                    {
                        robot_state_str = "OK";
                    }
                    Console.WriteLine("Zustand: " + robot_state_str);

                    Console.WriteLine("");

                    // Send data to MID ------------------------------------------
                    MachineData fmachineData = new MachineData(fmachineSchema);

                    fmachineData.Put("state_ok", robot_state_bool);
                    fmachineData.Put("q1", q1);
                    fmachineData.Put("q2", q2);
                    fmachineData.Put("Tuer offen", !robot_state_bool);
                    fmachineData.Put("Federmagazin leer", q1);
                    fmachineData.Put("Deckelmagazin leer", q2);
                    fmachineData.Put("Teil verloren", teil);


                    if (first_msg || (q1 != q1_old) || (q2 != q2_old) || (teil != teil_old) || (robot_state_bool != robot_state_bool_old))
                    {
                        DataMessage dataMessage1 = new DataMessage();
                        dataMessage1.Add(fmachineData);
                        Console.WriteLine("Sending Data Message: " + dataMessage1);

//                        client1.Send(dataMessage1);
                    }

                    q1_old               = q1;
                    q2_old               = q2;
                    teil_old             = teil;
                    robot_state_bool_old = robot_state_bool;
                    first_msg            = false;


                    Thread.Sleep(wait_ms);
                }//end while
            }
            catch (Exception e)
            {
                Console.WriteLine("Error at listening for state.....\n" + e.StackTrace);
            }
        }