code: def _connect(self):
          
  
          try:
              # FYI: the connection process might raise a
              # `ServerSelectionTimeoutError`, that is a subclass of
              # `ConnectionFailure`.
              # The presence of ca_cert, certfile, keyfile, crlfile implies the
              # use of certificates for TLS connectivity.
              if self.ca_cert is None or self.certfile is None or \
                      self.keyfile is None or self.crlfile is None:
                  client = pymongo.MongoClient(self.host,
                                               self.port,
                                               replicaset=self.replicaset,
                                               serverselectiontimeoutms=self.connection_timeout,
                                               ssl=self.ssl,
                                               **MONGO_OPTS)
                  if self.login is not None and self.password is not None:
                      client[self.dbname].authenticate(self.login, self.password)
              else:
                  logger.info('Connecting to MongoDB over TLS/SSL...')
                  client = pymongo.MongoClient(self.host,
                                               self.port,
                                               replicaset=self.replicaset,
                                               serverselectiontimeoutms=self.connection_timeout,
                                               ssl=self.ssl,
                                               ssl_ca_certs=self.ca_cert,
                                               ssl_certfile=self.certfile,
                                               ssl_keyfile=self.keyfile,
                                               ssl_pem_passphrase=self.keyfile_passphrase,
                                               ssl_crlfile=self.crlfile,
                                               ssl_cert_reqs=CERT_REQUIRED,
                                               **MONGO_OPTS)
                  if self.login is not None:
                      client[self.dbname].authenticate(self.login,
                                                       mechanism='MONGODB-X509')
  
              return client
  
          except (pymongo.errors.ConnectionFailure,
                  pymongo.errors.OperationFailure) as exc:
              logger.info('Exception in _connect(): {}'.format(exc))
              raise ConnectionError(str(exc)) from exc
          except pymongo.errors.ConfigurationError as exc:
              raise ConfigurationError from exc
docs: Try to connect to the database.
  
          Raises:
              :exc:`~ConnectionError`: If the connection to the database
                  fails.
              :exc:`~AuthenticationError`: If there is a OperationFailure due to
                  Authentication failure after connecting to the database.
              :exc:`~ConfigurationError`: If there is a ConfigurationError while
                  connecting to the database.