'Encode into GMCP messages.
Args:
cmdname (str): GMCP OOB command name.
args, kwargs (any): Arguments to OOB command.
Notes:
GMCP messages will be outgoing on the following
form (the non-JSON cmdname at the start is what
IRE games use, supposedly, and what clients appear
to have adopted):
[cmdname, [], {}]          -> cmdname
[cmdname, [arg], {}]       -> cmdname arg
[cmdname, [args],{}]       -> cmdname [args]
[cmdname, [], {kwargs}]    -> cmdname {kwargs}
[cmdname, [args, {kwargs}] -> cmdname [[args],{kwargs}]'

def encode_gmcp(self, cmdname, *args, **kwargs):

    if (not (args or kwargs)):
        gmcp_string = cmdname
    elif args:
        if (len(args) == 1):
            args = args[0]
        if kwargs:
            gmcp_string = ('%s    %s' % (cmdname, json.dumps([args, kwargs])))
        else:
            gmcp_string = ('%s    %s' % (cmdname, json.dumps(args)))
    else:
        gmcp_string = ('%s    %s' % (cmdname, json.dumps(kwargs)))
    return gmcp_string
