void _serverWatcher_Error(ServerWatcherErrorArgs args)
 {
     HandleError(args.Error);
 }
        private void Run()
        {
            while (!_switchoff)
            {
                while (!ApplicationState.Instance.SWatcherEnabled)
                {
                    lock (ApplicationState.Instance)
                    {
                        Monitor.Wait(ApplicationState.Instance);
                    }
                }
                try
                {
                    UserCredentials cred = UserCredentials.Instance;
                    using (TcpClient c = new TcpClient()) {
                        c.Connect(cred.Host, cred.Port);
                        using (StreamReader sr = new StreamReader(c.GetStream()))
                        using (StreamWriter sw = new StreamWriter(c.GetStream())) {

                            // Richiedi la registrazione per notifiche
                            NotificationRequest nr = new NotificationRequest(cred.Username, cred.AccessToken);
                            UserUtils.NetworkWriteLine(sw,nr.ToJSON());
                            string r = UserUtils.NetworkReadLine(sr);
                            var resp = Response.fromJson(r);
                            if (resp.Result == 401)
                                throw new UserLoginException(BadLoginResponse.fromJson(r).Msg);
                            else if (resp.Result != 200)
                                throw new Exception("Errore: il server ha rispost codice "+resp.Result); //TODO con eccezione e response ad-hoc

                            // Occorre attendere, virtualmente, anche fino all'infinito eventuali notifiche su questa socket.
                            c.ReceiveTimeout = 10000;
                            while (true)
                            {
                                try
                                {
                                    // Parte il comportamento da demone
                                    r = UserUtils.NetworkReadLine(sr);
                                    NotificationPong n = NotificationPong.FromJson(r);

                                    // Se il controllo raggiunge questa posizione significa che si è verificata qualche news
                                    // lato server.
                                    if (n.TargetVersion > SyncToken.Instance.VFSVersion)
                                    {
                                        PendingActions.Instance.QueuePull();
                                        ApplicationController.Instance.NotifyUI("News ricevute dal server: " + n.TargetVersion);
                                    }

                                }
                                catch (Exception e)
                                {
                                    // Ad eccezione avvenuta, ricominciamo da capo.
                                    ServerWatcherErrorArgs args = new ServerWatcherErrorArgs(e);
                                    OnError(args);
                                    break;
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    if (OnError != null)
                    {
                        ServerWatcherErrorArgs args = new ServerWatcherErrorArgs(e);
                        OnError(args);
                    }
                }
            }
        }