#include <curl/curl.h>

#define WAITMS(x) \
  struct timeval wait = { 0, (x) * 1000 };      \
  (void)select(0, NULL, NULL, NULL, &wait);

int main(void)
{

CURL *easy = curl_easy_init();
CURL *easyRaw = curl_easy_init();
CURLM *multi_handle = curl_multi_init();

//curl_easy_setopt(easy, CURLOPT_URL, "http://example.com");
curl_easy_setopt(easy, CURLOPT_URL, "127.0.0.1:8080");
curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
printf("easy created %p\r\n", easy);

//curl_easy_setopt(easyRaw, CURLOPT_URL, "http://example.com");
curl_easy_setopt(easyRaw, CURLOPT_URL, "127.0.0.1:8080");
curl_easy_setopt(easyRaw, CURLOPT_CONNECT_ONLY, 1L);
curl_easy_setopt(easyRaw, CURLOPT_VERBOSE, 1L);
printf("easyRaw created %p\r\n", easyRaw);


/* add the individual easy handle */
curl_multi_add_handle(multi_handle, easyRaw);
curl_multi_add_handle(multi_handle, easy);

int still_running, repeats = 0;
do {
  CURLMcode mc;
  int numfds;

  mc = curl_multi_perform(multi_handle, &still_running);

  if(mc == CURLM_OK ) {

    struct CURLMsg *m;

    int msgq = 0;
    m = curl_multi_info_read(multi_handle, &msgq);
    if(m && (m->msg == CURLMSG_DONE)) {
      CURL *e = m->easy_handle;
      printf("%p is done\r\n", e);
    }

    /* wait for activity, timeout or "nothing" */
    mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);
  }

  if(mc != CURLM_OK) {
    fprintf(stderr, "curl_multi failed, code %d.n", mc);
    break;
  }

  if(!numfds) {
    repeats++; /* count number of repeated zero numfds */
    if(repeats > 1) {
      WAITMS(100); /* sleep 100 milliseconds */
    }
  }
  else
    repeats = 0;

} while(still_running);

curl_multi_remove_handle(multi_handle, easyRaw);
curl_multi_remove_handle(multi_handle, easy);

curl_easy_cleanup(easyRaw);
curl_easy_cleanup(easy);

}
