private void Window_Closing(object sender, CancelEventArgs e)
        {
            var userPrefs = new UserPreferences
            {
                WindowHeight = Height,
                WindowWidth  = Width,
                WindowTop    = Top,
                WindowLeft   = Left,
                WindowState  = WindowState
            };

            Settings.Default.IsRemoteConnected = false;
            userPrefs.Save();
        }
        public MainWindow()
        {
            InitializeComponent();

            var userPrefs = new UserPreferences();

            conn = DBConnection.dbConnection();
            cmd  = new DBCommand(conn);

            try
            {
                InitSerialPort();
                sp.DataReceived  += Sp_DataReceived;
                sp.ErrorReceived += Sp_ErrorReceived;
                sp.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                InitSerialPort();
            }

            try
            {
                //sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //sck.Connect(Properties.Settings.Default.SocketAntriApotik, Properties.Settings.Default.PortAntriApotik);
                clientApotik = new SimpleTcpClient();
                clientApotik.Connect(Settings.Default.SocketAntriApotik,
                                     Settings.Default.PortAntriApotik);
                clientApotik.DataReceived += ClientApotik_DataReceived;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //InitSerialPort();
            }

            Height      = userPrefs.WindowHeight;
            Width       = userPrefs.WindowWidth;
            Top         = userPrefs.WindowTop;
            Left        = userPrefs.WindowLeft;
            WindowState = userPrefs.WindowState;
        }