static void trg_tls_update(TrgClient * tc, trg_tls * tls, gint serial)
{
    gchar *proxy;

    curl_easy_setopt(tls->curl, CURLOPT_PASSWORD,
                     trg_client_get_password(tc));
    curl_easy_setopt(tls->curl, CURLOPT_USERNAME,
                     trg_client_get_username(tc));
    curl_easy_setopt(tls->curl, CURLOPT_URL, trg_client_get_url(tc));

#ifndef CURL_NO_SSL
    if (trg_client_get_ssl(tc))
        curl_easy_setopt(tls->curl, CURLOPT_SSL_VERIFYPEER, 0);
#endif

    proxy = trg_client_get_proxy(tc);
    if (proxy) {
        curl_easy_setopt(tls->curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        curl_easy_setopt(tls->curl, CURLOPT_PROXY, proxy);
    }

    tls->serial = serial;
}
static CURL* get_curl(TrgClient *tc, guint http_class)
{
	TrgClientPrivate *priv = tc->priv;
	TrgPrefs *prefs = trg_client_get_prefs(tc);
	trg_tls *tls = get_tls(tc);
	CURL *curl = tls->curl;

    g_mutex_lock(&priv->configMutex);

    if (priv->configSerial > tls->serial || http_class != priv->http_class) {
    	gchar *proxy;

        curl_easy_reset(curl);

        curl_easy_setopt(curl, CURLOPT_USERAGENT, PACKAGE_NAME);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
                         &http_receive_callback);
#ifdef DEBUG
        if (g_getenv("TRG_CURL_VERBOSE") != NULL)
        	curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
#endif

        if (http_class == HTTP_CLASS_TRANSMISSION) {
        	curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *) tc);
        	curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        	curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &header_callback);
            curl_easy_setopt(curl, CURLOPT_PASSWORD,
                             trg_client_get_password(tc));
            curl_easy_setopt(curl, CURLOPT_USERNAME,
                             trg_client_get_username(tc));
            curl_easy_setopt(curl, CURLOPT_URL, trg_client_get_url(tc));
        }

    #ifndef CURL_NO_SSL
        if (trg_client_get_ssl(tc) && !trg_client_get_ssl_validate(tc)) {

            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
        }
    #endif

        proxy = trg_client_get_proxy(tc);
        if (proxy) {
            curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
            curl_easy_setopt(curl, CURLOPT_PROXY, proxy);
        }

        tls->serial = priv->configSerial;
        priv->http_class = http_class;
    }

    if (http_class == HTTP_CLASS_TRANSMISSION)
    	curl_easy_setopt(curl, CURLOPT_URL, trg_client_get_url(tc));

	curl_easy_setopt(curl, CURLOPT_TIMEOUT,
					 (long) trg_prefs_get_int(prefs, TRG_PREFS_KEY_TIMEOUT,
											  TRG_PREFS_CONNECTION));

    g_mutex_unlock(&priv->configMutex);

    /* Headers are set on each use, then freed, so make sure invalid headers aren't still around. */
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);

    return curl;

}