<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _loop(self):
 """Handle data."""

        request = {}
        connected = False
        while True:
            timeout = None
            sockets = [self.request_queue, self.signal]
            if not connected:
                try:
                    self._clear_request(request)
                    self._connect()
                    self._soc.send(_build_request(
                        {'cmd': cmd.CMD_MESSAGE_LIST}))
                    self._soc.send(_build_request(
                        {'cmd': cmd.CMD_MESSAGE_CDR_AVAILABLE}))
                    connected = True
                except ConnectionRefusedError:
                    timeout = 5.0
            if connected:
                sockets.append(self._soc)

            readable, _writable, _errored = select.select(
                sockets, [], [], timeout)

            if self.signal in readable:
                break

            if self._soc in readable:
                # We have incoming data
                try:
                    command, msg = self._recv_msg()
                    self._handle_msg(command, msg, request)
                except (RuntimeError, ConnectionResetError):
                    logging.warning("Lost connection")
                    connected = False
                    self._clear_request(request)

            if self.request_queue in readable:
                request = self.request_queue.get()
                self.request_queue.task_done()
                if not connected:
                    self._clear_request(request)
                else:
                    if (request['cmd'] == cmd.CMD_MESSAGE_LIST and
                            self._status and
                            (not self._callback or 'sync' in request)):
                        self.result_queue.put(
                            [cmd.CMD_MESSAGE_LIST, self._status])
                        request = {}
                    else:
                        self._soc.send(_build_request(request))