'Reads the connection property for the host, and returns the
correct connection object from the list of connection plugins'

def _get_connection(self, variables, templar):

    if (self._task.delegate_to is not None):
        for i in list(variables.keys()):
            if (isinstance(i, string_types) and i.startswith('ansible_') and i.endswith('_interpreter')):
                del variables[i]
        delegated_vars = variables.get('ansible_delegated_vars', dict()).get(self._task.delegate_to, dict())
        if isinstance(delegated_vars, dict):
            for i in delegated_vars:
                if (isinstance(i, string_types) and i.startswith('ansible_') and i.endswith('_interpreter')):
                    variables[i] = delegated_vars[i]
    if (C.USE_PERSISTENT_CONNECTIONS or getattr(self, 'FORCE_PERSISTENT_CONNECTION', False)):
        conn_type = 'persistent'
    else:
        conn_type = self._play_context.connection
    connection = self._shared_loader_obj.connection_loader.get(conn_type, self._play_context, self._new_stdin)
    if (not connection):
        raise AnsibleError(("the    connection    plugin    '%s'    was    not    found" % conn_type))
    if self._play_context.accelerate:
        display.deprecated('Accelerated    mode    is    deprecated.    Consider    using    SSH    with    ControlPersist    and    pipelining    enabled    instead', version='2.6')
        ssh_connection = connection
        handler = self._shared_loader_obj.action_loader.get('normal', task=self._task, connection=ssh_connection, play_context=self._play_context, loader=self._loader, templar=templar, shared_loader_obj=self._shared_loader_obj)
        key = key_for_hostname(self._play_context.remote_addr)
        accelerate_args = dict(password=base64.b64encode(key.__str__()), port=self._play_context.accelerate_port, minutes=C.ACCELERATE_DAEMON_TIMEOUT, ipv6=self._play_context.accelerate_ipv6, debug=self._play_context.verbosity)
        connection = self._shared_loader_obj.connection_loader.get('accelerate', self._play_context, self._new_stdin)
        if (not connection):
            raise AnsibleError(("the    connection    plugin    '%s'    was    not    found" % conn_type))
        try:
            connection._connect()
        except AnsibleConnectionFailure:
            display.debug('connection    failed,    fallback    to    accelerate')
            res = handler._execute_module(module_name='accelerate', module_args=accelerate_args, task_vars=variables, delete_remote_tmp=False)
            display.debug(res)
            connection._connect()
    return connection
