title: How to fix cURL error "SSL connection timeout" only on the first time the script is called?
body: <p>I'm facing a strange problem using cURL with PHP on a Windows server. I have a very basic code:</p>
  
  <pre><code>private function curlConnection($method, $url, $timeout, $charset, array $data = null)
              {
  
                  if (strtoupper($method) === 'POST') {
                      $postFields = ($data ? http_build_query($data, '', '&amp;') : "");
                      $contentLength = "Content-length: " . strlen($postFields);
                      $methodOptions = array(
                          CURLOPT_POST =&gt; true,
                          CURLOPT_POSTFIELDS =&gt; $postFields,
                      );
                  } else {
                      $contentLength = null;
                      $methodOptions = array(
                          CURLOPT_HTTPGET =&gt; true
                      );
                  }
  
  
                  $options = array(
                  CURLOPT_HTTPHEADER =&gt; array(
                      "Content-Type: application/x-www-form-urlencoded; charset=" . $charset,
                      $contentLength,
                      'lib-description: php:' . PagSeguroLibrary::getVersion(),
                      'language-engine-description: php:' . PagSeguroLibrary::getPHPVersion()
                  ),
                  CURLOPT_URL =&gt; $url,
                  CURLOPT_RETURNTRANSFER =&gt; true,
                  CURLOPT_HEADER =&gt; true,
                  CURLOPT_SSL_VERIFYPEER =&gt; false,
                  CURLOPT_CONNECTTIMEOUT =&gt; $timeout
                  );
  
                  $options = ($options + $methodOptions);
  
                  $curl = curl_init();
                  curl_setopt_array($curl, $options);
                  $resp = curl_exec($curl);
                  $info = curl_getinfo($curl);
                  $error = curl_errno($curl);
                  $errorMessage = curl_error($curl);
                  curl_close($curl);
                  $this-&gt;setStatus((int) $info['http_code']);
                  $this-&gt;setResponse((String) $resp);
                  if ($error) {
                      throw new Exception("CURL can't connect: $errorMessage");
                  } else {
                      return true;
                  }
              }
  </code></pre>
  
  <p>The problem is that the first time this script is called, the response is always this: string(22) "SSL connection timeout".</p>
  
  <p>Subsequent calls to the script output the desired result, but, if I wait a couple of minutes before calling the script again, the timeout issue happens again.</p>
  
  <p>So, steps to reproduce the "error":</p>
  
  <ol>
  <li>Call the script -> SSL connection timeout </li>
  <li>Call the script again -> works fine</li>
  <li>Call the script one more time -> works fine</li>
  <li>Call the script n more times -> works fine</li>
  <li>Wait 10 minutes</li>
  <li>Call the script -> SSL connection timeout</li>
  <li>Call the script n more times again -> works fine</li>
  </ol>
  
  <p>If I call any other script the response is immediate, even after a period of inactivity, so this behaviour only happen when cURL is involved.</p>
  
  <p>PHP - 5.2.17
  CURL - libcurl/7.16.0 OpenSSL/0.9.8q zlib/1.2.3
  The server is running Windows 2012 with IIS 8, latest upgrades, running PHP on FastCGI.</p>
  
  <p>Does anyone have any idea on how I can solve this?</p>
  
  <p>Thanks.</p>