function mcCmdLoop (mcConnect, $rootScope){
    "use strict";

    window.idMcPingTimer = null;

    function RunCMD(CMD_Block, debug) {
        if (debug) {
            console.log("CMD_Block:\n" + CMD_Block);
        }

        if (CMD_Block && CMD_Block.length > 0){
            CMD_Block = mcService.StringToObj(CMD_Block);

            for (var CMD_Numb in CMD_Block){
                $rootScope.ProcessCMD([CMD_Numb, CMD_Block[CMD_Numb]]);
            }
        }
    }

    function cmdPing (sID){
        $rootScope.SendCMDToServer([mcConst._CMD_.Ping, sID]);
    }



    return function StartPingTimer(_time, sID, opt){ // this.StartPingTimer =
        var self = {};
        var options =  opt || {
            onError : null,
            onStop  : null,
            onWarn  : null
        };

        mcConnect.ClearAllCallBackFunctionByCMD();
        mcConnect.LeaveCallBacks = true;

        mcConnect.SetCallBackFunctionByCMD(mcConst._CMD_.Ping, function(){
            var data = this;

            if ((data !== "{}") && (data.length > 0)) {
                RunCMD(data);
            }
        });

        if (mcConst.SessionID)
            window.idMcPingTimer = setInterval(function(){
                if (mcConst.SessionID) {
                    cmdPing(mcConst.SessionID);
                } else {
                    console.error("mcConst.SessionID is not defined. StopPingTimer()");

                    self.StopPingTimer();
                }
            }, _time);

        self.StopPingTimer = function(){
            if (window.idMcPingTimer !== null){
                clearInterval(window.idMcPingTimer);

                window.idMcPingTimer = null;

                mcConnect.ClearAllCallBackFunctionByCMD();

                if (options.onStop) {
                    options.onStop();
                }
            }
        };

        mcConnect.OnError = function(err){
            if (console) {
                console.warn('An error occurred: Ping-Timer has been Stopped');
            }

            self.StopPingTimer();

            if (options.onError){
                options.onError(err);
            }
        };

        return self;
    }
}
