<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, app_id, wait=True, security=None):
 """Connect to a running application. Parameters app_id : str The id of the application. wait : bool, optional If true [default], blocks until the application starts. If False, will raise a ``ApplicationNotRunningError`` immediately if the application isn't running. security : Security, optional The security configuration to use to communicate with the application master. Defaults to the global configuration. Returns ------- app_client : ApplicationClient Raises ------ ApplicationNotRunningError If the application isn't running. """

        if wait:
            resp = self._call('waitForStart', proto.Application(id=app_id))
        else:
            resp = self._call('getStatus', proto.Application(id=app_id))
        report = ApplicationReport.from_protobuf(resp)
        if report.state is not ApplicationState.RUNNING:
            raise ApplicationNotRunningError(
                "%s is not running. Application state: "
                "%s" % (app_id, report.state))

        if security is None:
            security = self.security

        return ApplicationClient('%s:%d' % (report.host, report.port),
                                 app_id,
                                 security=security)