<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _connect(self):
 """Connects to the ec2 cloud provider :return: :py:class:`boto.ec2.connection.EC2Connection` :raises: Generic exception on error """

        # check for existing connection
        if self._ec2_connection:
            return self._ec2_connection

        if not self._vpc:
            vpc_connection = None

        try:
            log.debug("Connecting to ec2 host %s", self._ec2host)
            region = ec2.regioninfo.RegionInfo(name=self._region_name,
                                               endpoint=self._ec2host)

            # connect to webservice
            ec2_connection = boto.connect_ec2(
                aws_access_key_id=self._access_key,
                aws_secret_access_key=self._secret_key,
                is_secure=self._secure,
                host=self._ec2host, port=self._ec2port,
                path=self._ec2path, region=region)
            log.debug("EC2 connection has been successful.")

            if self._vpc:
                vpc_connection = boto.connect_vpc(
                    aws_access_key_id=self._access_key,
                    aws_secret_access_key=self._secret_key,
                    is_secure=self._secure,
                    host=self._ec2host, port=self._ec2port,
                    path=self._ec2path, region=region)
                log.debug("VPC connection has been successful.")

                for vpc in vpc_connection.get_all_vpcs():
                    log.debug("Checking whether %s matches %s/%s" %
                        (self._vpc, vpc.tags['Name'], vpc.id))
                    if self._vpc in [vpc.tags['Name'], vpc.id]:
                        self._vpc_id = vpc.id
                        if self._vpc != self._vpc_id:
                            log.debug("VPC %s matches %s" %
                                (self._vpc, self._vpc_id))
                        break
                else:
                    raise VpcError('VPC %s does not exist.' % self._vpc)

            # list images to see if the connection works
            # images = self._ec2_connection.get_all_images()
            # log.debug("%d images found on cloud %s",
            #           len(images), self._ec2host)

        except Exception as e:
            log.error("connection to ec2 could not be "
                      "established: message=`%s`", str(e))
            raise

        self._ec2_connection, self._vpc_connection = (
            ec2_connection, vpc_connection)
        return self._ec2_connection