'Communicate with the JSON server.'

def json_request(self, request, wait_for_response=False):

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(5)
    try:
        sock.connect((self._host, self._port))
    except OSError:
        sock.close()
        return False
    sock.send(bytearray((json.dumps(request) + '\n'), 'utf-8'))
    try:
        buf = sock.recv(4096)
    except socket.timeout:
        sock.close()
        return False
    buffering = True
    while buffering:
        if ('\n' in str(buf, 'utf-8')):
            response = str(buf, 'utf-8').split('\n')[0]
            buffering = False
        else:
            try:
                more = sock.recv(4096)
            except socket.timeout:
                more = None
            if (not more):
                buffering = False
                response = str(buf, 'utf-8')
            else:
                buf += more
    sock.close()
    return json.loads(response)
