def balance_load(self, raw_socket):
    self._logger.debug('=> %s', messages.LoadBalanceRequest())
    raw_socket.sendall(messages.LoadBalanceRequest().get_message())
    response = raw_socket.recv(1)
    if (response == b'Y'):
        size = unpack('!I', raw_socket.recv(4))[0]
        if (size < 4):
            err_msg = 'Bad message size: {0}'.format(size)
            self._logger.error(err_msg)
            raise errors.MessageError(err_msg)
        res = BackendMessage.from_type(type_=response, data=raw_socket.recv((size - 4)))
        self._logger.debug('<= %s', res)
        host = res.get_host()
        port = res.get_port()
        self._logger.info('Load balancing to host "{0}" on port {1}'.format(host, port))
        peer = raw_socket.getpeername()
        (socket_host, socket_port) = (peer[0], peer[1])
        if ((host == socket_host) and (port == socket_port)):
            self._logger.info('Already connecting to host "{0}" on port {1}. Ignore load balancing.'.format(host, port))
            return raw_socket
        self.address_list.push(host, port)
        raw_socket.close()
        raw_socket = self.establish_socket_connection(self.address_list)
    else:
        self._logger.debug('<= LoadBalanceResponse: %s', response)
        self._logger.warning('Load balancing requested but not supported by server')
    return raw_socket
def balance_load(self, raw_socket):
    self._logger.debug('=> %s', messages.LoadBalanceRequest())
    raw_socket.sendall(messages.LoadBalanceRequest().get_message())
    response = raw_socket.recv(1)
    if (response == b'Y'):
        size = unpack('!I', raw_socket.recv(4))[0]
        if (size < 4):
            err_msg = 'Bad message size: {0}'.format(size)
            self._logger.error(err_msg)
            raise errors.MessageError(err_msg)
        res = BackendMessage.from_type(type_=response, data=raw_socket.recv((size - 4)))
        self._logger.debug('<= %s', res)
        host = res.get_host()
        port = res.get_port()
        self._logger.info('Load balancing to host "{0}" on port {1}'.format(host, port))
        peer = raw_socket.getpeername()
        (socket_host, socket_port) = (peer[0], peer[1])
        if ((host == socket_host) and (port == socket_port)):
            self._logger.info('Already connecting to host "{0}" on port {1}. Ignore load balancing.'.format(host, port))
            return raw_socket
        self.address_list.push(host, port)
        raw_socket.close()
        raw_socket = self.establish_socket_connection(self.address_list)
    else:
        self._logger.debug('<= LoadBalanceResponse: %s', response)
        self._logger.warning('Load balancing requested but not supported by server')
    return raw_socket