code: def connection(self, hostname=None, read_preference=pymongo.ReadPreference.PRIMARY, timeout=300):
          
          logger.debug("connection({hostname}, {read_preference}, {timeout})".format(**locals()))
          t_start = time.time()
          servers = hostname or ",".join(self.server_map.values())
          while True:
              try:
                  if hostname is None:
                      c = pymongo.MongoReplicaSetClient(
                          servers, replicaSet=self.repl_id,
                          read_preference=read_preference,
                          socketTimeoutMS=self.socket_timeout,
                          w=self._write_concern, fsync=True, **self.kwargs)
                      connected(c)
                      if c.primary:
                          self._authenticate_client(c)
                          return c
                      raise pymongo.errors.AutoReconnect("No replica set primary available")
                  else:
                      logger.debug("connection to the {servers}".format(**locals()))
                      c = pymongo.MongoClient(
                          servers, socketTimeoutMS=self.socket_timeout,
                          w=self._write_concern, fsync=True, **self.kwargs)
                      connected(c)
                      self._authenticate_client(c)
                      return c
              except (pymongo.errors.PyMongoError):
                  exc_type, exc_value, exc_tb = sys.exc_info()
                  err_message = traceback.format_exception(exc_type, exc_value, exc_tb)
                  logger.error("Exception {exc_type} {exc_value}".format(**locals()))
                  logger.error(err_message)
                  if time.time() - t_start > timeout:
                      raise pymongo.errors.AutoReconnect("Couldn't connect while timeout {timeout} second".format(**locals()))
                  time.sleep(1)
docs: return MongoReplicaSetClient object if hostname specified
          return MongoClient object if hostname doesn't specified
          Args:
              hostname - connection uri
              read_preference - default PRIMARY
              timeout - specify how long, in seconds, a command can take before server times out.