public void ConnectOrWait()
        {
            if (connecting)
                return;
            lock (connectLock)
            {
                connecting = true;
                String response;
                while (true)
                {
                tryagain:
                    Program.form.BeginInvoke(new Action(() => Program.form.setStatus("Waiting for security service")));
                    try
                    {
                        while (true)
                        {
                            try
                            {
                                if (mainClient == null)
                                {
                                    mainClient = new ServiceClient();
                                }
                                else
                                {
                                    mainClient.Reconnect();
                                }

                            }
                            catch (Exception e)
                            {
                                System.Threading.Thread.Sleep(10000);
                                continue;
                            }

                            response = mainClient.sendMessage("BEGIN");
                            if (response.StartsWith("OK"))
                            {
                                break;
                            }

                            System.Threading.Thread.Sleep(10000);
                        }
                        Program.form.BeginInvoke(new Action(() => Program.form.setStatus("Connected, waiting server")));

                        while (true)
                        {
                            response = mainClient.sendMessage("HASKEY");

                            if (!response.StartsWith("OK"))
                            {
                                System.Threading.Thread.Sleep(10000);
                                goto tryagain;
                            }

                            if (response.StartsWith("OK YES"))
                            {
                                break;
                            }
                            System.Threading.Thread.Sleep(10000);
                        }

                        Program.form.BeginInvoke(new Action(() => Program.form.setStatus("Running")));
                        ready = true;
                        break;
                    }
                    catch (Exception e)
                    {
                        //MessageBox.Show(Program.form, e.Message + e.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        continue;
                    }
                    finally
                    {
                        connecting = false;
                    }
                }
            }
        }