public function autoselect_host()
    {
        $default_host = $this->config->get('default_host');
        $host         = null;

        if (is_array($default_host)) {
            $post_host = rcube_utils::get_input_value('_host', rcube_utils::INPUT_POST);
            $post_user = rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST);

            list(, $domain) = explode('@', $post_user);

            // direct match in default_host array
            if ($default_host[$post_host] || in_array($post_host, array_values($default_host))) {
                $host = $post_host;
            }
            // try to select host by mail domain
            else if (!empty($domain)) {
                foreach ($default_host as $storage_host => $mail_domains) {
                    if (is_array($mail_domains) && in_array_nocase($domain, $mail_domains)) {
                        $host = $storage_host;
                        break;
                    }
                    else if (stripos($storage_host, $domain) !== false || stripos(strval($mail_domains), $domain) !== false) {
                        $host = is_numeric($storage_host) ? $mail_domains : $storage_host;
                        break;
                    }
                }
            }

            // take the first entry if $host is still not set
            if (empty($host)) {
                $key  = key($default_host);
                $host = is_numeric($key) ? $default_host[$key] : $key;
            }
        }
        else if (empty($default_host)) {
            $host = rcube_utils::get_input_value('_host', rcube_utils::INPUT_POST);
        }
        else {
            $host = rcube_utils::parse_host($default_host);
        }

        return $host;
    }