#include <stdio.h>
#ifdef _WIN32
  #include <winsock.h>
#else
  #include <socket.h>
#endif
#include <time.h>

int
main (int argc, char * argv[])
{
  time_t start, end;
  struct sockaddr_in addr;
  char buffer[16];
  double elapsed;
  int x;

#ifdef _WIN32
  /* start up winsock */
  WSADATA WSAData;
  WSAStartup(0x0101, &WSAData);
#endif

  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl ((long) 127<<24 | (long) 1);
  addr.sin_port = htons (9999);

  time (&start);

  for (x=0; x<1000; x++) {
    SOCKET s = socket (AF_INET, SOCK_STREAM, 0);
    if (s == INVALID_SOCKET) {
      fprintf (stderr, "socket() => %d\n", WSAGetLastError ());
    }
    connect (s, (struct sockaddr *)(&addr), sizeof(addr));
    recv (s, buffer, 5, 0);
    send (s, buffer, 5, 0);
    closesocket (s);
  }

  time (&end);

  elapsed = difftime (end, start);
  /* for some reason this is _always_ in whole seconds */
  fprintf (stderr, "%0.3f seconds\n", elapsed);

  return 0;

}
