def _connect(uri):
    '''
    uri = user/password@host[:port]/sid[servicename as {sysdba|sysoper}]
     or
    uri = sid[ as {sysdba|sysoper}]
     (this syntax only makes sense on non-Windows minions, ORAHOME is taken from oratab)

    Return cx_Oracle.Connection instance
    '''
    # cx_Oracle.Connection() does not support 'as sysdba' syntax
    uri_l = uri.rsplit(' as ', 1)
    if len(uri_l) == 2:
        credentials, mode = uri_l
        mode = MODE[mode]
    else:
        credentials = uri_l[0]
        mode = 0
    # force UTF-8 client encoding
    os.environ['NLS_LANG'] = '.AL32UTF8'
    if '@' in uri:
        serv_name = False
        userpass, hostportsid = credentials.split('@')
        user, password = userpass.split('/')
        hostport, sid = hostportsid.split('/')
        if 'servicename' in sid:
            serv_name = True
            sid = sid.split('servicename')[0].strip()
        hostport_l = hostport.split(':')
        if len(hostport_l) == 2:
            host, port = hostport_l
        else:
            host = hostport_l[0]
            port = 1521
        log.debug('connect: %s', (user, password, host, port, sid, mode))
        if serv_name:
            conn = cx_Oracle.connect(user, password, cx_Oracle.makedsn(host, port, service_name=sid), mode)
        else:
            conn = cx_Oracle.connect(user, password, cx_Oracle.makedsn(host, port, sid), mode)
    else:
        sid = uri.rsplit(' as ', 1)[0]
        orahome = _parse_oratab(sid)
        if orahome:
            os.environ['ORACLE_HOME'] = orahome
        else:
            raise CommandExecutionError('No uri defined and SID {0} not found in oratab'.format(sid))
        os.environ['ORACLE_SID'] = sid
        log.debug('connect: %s', (sid, mode))
        conn = cx_Oracle.connect(mode=MODE['sysdba'])
    conn.outputtypehandler = _unicode_output
    return conn