private object processpayload(object data)
        {
            ReplicationPacket p = (ReplicationPacket)data;

            if (Authenticate(p) == false)
                return new ReturnPacket(false, "Authentication failed");

            ReturnPacket ret = new ReturnPacket(true);
            try
            {
                switch (p.command)
                {
                    case "getbranchconfig":
                        ret.OK = true;
                        ret.Data = GetBranchConfig(p.branchname);
                        break;
                    case "getpackageforbranch":
                        ret.OK = true;
                        ReplicationPacket pack = GetPackageForBranch(p);
                        ret.Data = pack;
                        break;
                    case "packageforhq":
                        ret.OK = PackageForHQ(p);
                        break;
                    case "hqpackageok":
                        ret.OK = true;
                        File.Delete(_OutboxPath + _S + p.branchname + _S + p.filename);
                        // set last rec on hq
                        _branchLastDocs.Add(p.branchname.ToLower(), p.lastrecord);
                        WriteBranchCounters();
                        break;
                }
            }
            catch (Exception ex)
            {
                ret.OK = false;
                _log.Error(ex);
            }
            return ret;
        }
        private object processpayload(object data)
        {
            Packet p = (Packet)data;
            ReturnPacket ret = new ReturnPacket(true);

            if (Authenticate(p) == false)
                return new ReturnPacket(false, "Authentication failed");
            if (p.Command == "_close")
            {
                _connectedClients.Remove(p.ClientID);
                return ret;
            }
            else
                _connectedClients.Add(p.ClientID, true);

            try
            {
                Handler d = null;
                if (_handlers.TryGetValue(p.Command, out d))
                    d(p, ret);
                else
                    _log.Error("Command handler not found : " + p.Command);
            }
            catch (Exception ex)
            {
                ret.OK = false;
                _log.Error(ex);
            }
            return ret;
        }
        private object processpayload(object data)
        {
            Packet p = (Packet)data;

            if (Authenticate(p) == false)
            {
                //JSR - introduce an Authenticate Command
                switch (p.Command)
                {
                    case "authenticate":
                        //This is true as the ReturnPacket is valid, but includes a message.
                        var rtn = new ReturnPacket(true, "Authentication failed");
                        rtn.OK = true;
                        rtn.Data = "Some Dummy Data";
                        return rtn;
                        //break;
                }
                //JSR - cater for the original situation where some other command is executed.
                return new ReturnPacket(false, "Authentication failed");
            }

            ReturnPacket ret = new ReturnPacket(true);
            try
            {
                object[] param = null;

                switch (p.Command)
                {
                    case "save":
                        var m = GetSave(p.Data.GetType());
                        m.Invoke(_raptor, new object[] { p.Docid, p.Data });
                        break;
                    case "savebytes":
                        ret.OK = _raptor.SaveBytes(p.Docid, (byte[])p.Data);
                        break;
                    case "querytype":
                        param = (object[])p.Data;

                        //Since the RowSchema is nested and used like SalesInvoiceView.RowSchema
                        //the DeclaringType is used.
                        Type t = Type.GetType((string) param[0]);

                        var a = t.Name; //RowSchema
                        var b = t.DeclaringType.Name; //SalesInvoiceView
                        var c = t.ReflectedType.Name; //SalesInvoiceView
                        var d = t.FullName; //RaptorDBTest1Views.SalesInvoiceView+RowSchema

                        var viewname = t.DeclaringType.Name;
                        if (String.IsNullOrEmpty(viewname))
                        {
                            ret.OK = false;
                            ret.Error = "View Not Found.";
                            ret.Data = "Some Dummy Data";
                        }
                        else
                        {
                            ret.OK = true;
                            ret.Data = _raptor.Query(viewname, (string)param[1], p.Start, p.Count, p.OrderBy);
                        }
                        break;
                    case "querystr":
                        ret.OK = true;
                        ret.Data = _raptor.Query(p.Viewname, (string)p.Data, p.Start, p.Count, p.OrderBy);
                        break;
                    case "fetch":
                        ret.OK = true;
                        ret.Data = _raptor.Fetch(p.Docid);
                        break;
                    case "fetchbytes":
                        ret.OK = true;
                        ret.Data = _raptor.FetchBytes(p.Docid);
                        break;
                    case "backup":
                        ret.OK = _raptor.Backup();
                        break;
                    case "delete":
                        ret.OK = _raptor.Delete(p.Docid);
                        break;
                    case "deletebytes":
                        ret.OK = _raptor.DeleteBytes(p.Docid);
                        break;
                    case "restore":
                        ret.OK = true;
                        Task.Factory.StartNew(() => _raptor.Restore());
                        break;
                    case "adduser":
                        param = (object[])p.Data;
                        ret.OK = AddUser((string)param[0], (string)param[1], (string)param[2]);
                        break;
                    case "getusers":
                        ret.OK = true;
                        List<String> users = new List<string>();
                        foreach(var user in _users)
                            users.Add(user.Key);
                        //Only seems to work passing back an Object[]
                        ret.Data = users.ToArray<Object>();
                        break;
                    case "serverside":
                        param = (object[])p.Data;
                        ret.OK = true;
                        ret.Data = _raptor.ServerSide(GetServerSideFuncCache(param[0].ToString(), param[1].ToString()), param[2].ToString());
                        break;
                    case "fulltext":
                        param = (object[])p.Data;
                        ret.OK = true;
                        ret.Data = _raptor.FullTextSearch("" + param[0]);
                        break;
                    case "counttype":
                        // count type
                        param = (object[])p.Data;
                        Type tt = Type.GetType((string)param[0]);
                        string viewname2 = _raptor.GetViewName(tt);
                        //if (viewname2 == "") viewname2 = _raptor.GetView((string)param[0]);
                        ret.OK = true;
                        ret.Data = _raptor.Count(viewname2, (string)param[1]);
                        break;
                    case "countstr":
                        // count str
                        ret.OK = true;
                        ret.Data = _raptor.Count(p.Viewname, (string)p.Data);
                        break;
                    case "gcount":
                        //param = (object[])p.Data;
                        Type ttt = Type.GetType(p.Viewname);
                        string viewname3 = _raptor.GetViewName(ttt);
                        //if (viewname3 == "") viewname3 = _raptor.GetView(p.Viewname);
                        ret.OK = true;
                        ret.Data = _raptor.Count(viewname3, (string)p.Data);
                        break;
                    case "dochistory":
                        ret.OK = true;
                        ret.Data = _raptor.FetchHistory(p.Docid);
                        break;
                    case "filehistory":
                        ret.OK = true;
                        ret.Data = _raptor.FetchBytesHistory(p.Docid);
                        break;
                    case "fetchversion":
                        ret.OK = true;
                        ret.Data = _raptor.FetchVersion((int)p.Data);
                        break;
                    case "fetchfileversion":
                        ret.OK = true;
                        ret.Data = _raptor.FetchBytesVersion((int)p.Data);
                        break;
                    case "checkassembly":
                        ret.OK = true;
                        string typ = "";
                        ret.Data = _raptor.GetAssemblyForView(p.Viewname, out typ);
                        ret.Error = typ;
                        break;
                    case "getviews":
                        ret.OK = true;
                        List<String> views = new List<string>();
                        foreach(var view in _raptor.GetViews())
                            views.Add(view.Name);
                        ret.Data = views.ToArray<Object>();
                        break;
                }
            }
            catch (Exception ex)
            {
                ret.OK = false;
                //JSR
                //
                ret.Error = ex.Message;
                log.Error(ex);
            }
            return ret;
        }