code: def _connect_to_device(self, uuid, key, client):
          
  
          slug = self._build_device_slug(uuid)
          message = {'client': client, 'type': 'response', 'operation': 'connect'}
  
          self._logger.info("Connection attempt for device %d", uuid)
  
          # If someone is already connected, fail the request
          if uuid in self._connections:
              message['success'] = False
              message['failure_reason'] = 'Someone else is connected to the device'
  
              self._publish_status(slug, message)
              return
  
          # Otherwise try to connect
          resp = yield self._manager.connect(uuid)
          message['success'] = resp['success']
          if resp['success']:
              conn_id = resp['connection_id']
              self._connections[uuid] = {'key': key, 'client': client, 'connection_id': conn_id, 'last_touch': monotonic(),
                                         'script': [], 'trace_accum': bytes(), 'last_trace': None, 'trace_scheduled': False,
                                         'last_progress': None}
          else:
              message['failure_reason'] = resp['reason']
              self._connections[uuid] = {}
  
          connection = self._connections[uuid]
          connection['report_monitor'] = self._manager.register_monitor(uuid, ['report'], self._notify_report)
          connection['trace_monitor'] = self._manager.register_monitor(uuid, ['trace'], self._notify_trace)
  
          self._publish_status(slug, message)
docs: Connect to a device given its uuid
  
          Args:
              uuid (int): The unique id of the device
              key (string): A 64 byte string used to secure this connection
              client (string): The client id for who is trying to connect
                  to the device.