private void MainThread()
        {
            eventLog1.BeginInit();

            DataRow dr      = null;
            bool    bLogged = false;

            TvMain = new STKTVMain.TVMain();

            try
            {
                if (TvMain.Init() == true)
                {
                    bLogged = true;
                }
                else
                {
                    WarningReport("Unable to login, check credentials");
                    InfoReport(TvMain.GetEnvInfo());
                    return;
                }
            }
            catch (Exception Ex)
            {
                ErrorReport("Login failed, try again... " + Ex.Message);
                InfoReport(TvMain.GetEnvInfo());
            }

            //InfoReport("DB Initialization OK");
            System.Data.DataTable oRS;
            if (bLogged)
            {
                oRS = null;
                String q;
                q = "select plancall.*,npip,nppassword,ipport,transport,sysdate ServerDate from bdevices join plancall on bdevices.id_bd=plancall.id_bd where  ( nplock is null or nplock <sysdate ) and npquery=1 " +
                    " and bdevices.id_bd=" + DivID.ToString() + " ";
                oRS = TvMain.QuerySelect(q);
                if (oRS != null)
                {
                    if (oRS.Rows.Count > 0)
                    {
                        try
                        {
                            dr = oRS.Rows[0];
                            DeviceThread(dr);
                        }
                        catch (Exception Ex)
                        {
                            ErrorReport("Прибор ID=  " + DivID.ToString() + " error:" + Ex.Message);
                            dr = null;
                        }
                    }
                    oRS = null;
                }



                try
                {
                    //InfoReport("Closing Device thread...");

                    //dr = null;

                    TvMain.ClearDuration();
                    // close transport
                    TvMain.DeviceClose();
                    TvMain.CloseDBConnection();
                    TvMain = null;
                    eventLog1.Dispose();
                    return;
                }
                catch (Exception Ex)
                {
                    ErrorReport("Closing DeviceThread error:" + Ex.Message);
                }
            }
        }
        private void Executor()
        {
            eventLog1.BeginInit();
            DataRow dr = null;
            bool bLogged = false;
            TvMain = new STKTVMain.TVMain();

            try
            {

                if (TvMain.Init() == true)
                {
                    bLogged = true;
                }
                else
                {
                    WarningReport("Unable to login, check credentials");
                    InfoReport(TvMain.GetEnvInfo());
                    return;
                }
            }
            catch (Exception Ex)
            {

                ErrorReport("Login failed, try again... " + Ex.Message);
                InfoReport(TvMain.GetEnvInfo());
            }

            //InfoReport("DB Initialization OK");
            System.Data.DataTable oRS;
            System.Data.DataTable devRS;
            if (bLogged)
            {

                devRS = TvMain.QuerySelect("select id_bd from bdevices where transport=5 and callerid='" + aSocket.callerID + "'");


                if (devRS.Rows.Count == 1)
                {
                    DivID = Convert.ToInt32(devRS.Rows[0]["id_bd"]);
                    oRS = null;


                    //if (TvMain.LockDevice(DivID, 6000, false))
                    {

                    oRS = TvMain.QuerySelect("select plancall.*,npip,nppassword,ipport,transport,sysdate ServerDate from bdevices join plancall on bdevices.id_bd=plancall.id_bd  where bdevices.id_bd=" + DivID.ToString());
                    if (oRS != null)
                    {

                        if (oRS.Rows.Count > 0)
                        {
                            try
                            {
                                dr = oRS.Rows[0];
                                ProcessPlan(dr);

                            }
                            catch (Exception Ex)
                            {
                                ErrorReport("Прибор ID=  " + DivID.ToString() + " error:" + Ex.Message);
                                dr = null;
                            }
                        }
                        oRS = null;

                    }

                    }


                    try
                    {
                        //InfoReport("Closing Device thread...");

                        //dr = null;

                        TvMain.ClearDuration();
                        // close transport
                        TvMain.DeviceClose();

                        if (dr != null)
                        {
                            AnalizeDevice(dr);
                        }
                        TvMain.CloseDBConnection();
                        TvMain = null;
                        eventLog1.Dispose();
                        return;
                    }
                    catch (Exception Ex)
                    {
                        ErrorReport("Closing ProcessPlan error:" + Ex.Message);
                    }

                }
                else
                {
                    if (devRS.Rows.Count ==0)
                        ErrorReport("Не обнаружен прибор для АССВ устройства с идентификатором: " + aSocket.callerID);
                    else
                        ErrorReport("Обнаружено " +devRS.Rows.Count.ToString() + " приборов для АССВ устройства с идентификатором: " + aSocket.callerID);
                }
            }


        }