#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>

typedef int SOCKET;
#define closesocket(s) close (s)
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1

#endif

#include <glib.h>

int
main (int argc, char **argv)
{
#ifdef _WIN32
  WSADATA wsa_data;
#endif
  SOCKET s, t;
  struct sockaddr_in addr;
  gint rc;
  gchar buffer[1];

#ifdef _WIN32
  g_assert (WSAStartup(0x0202, &wsa_data) == 0);
#endif

  s = socket (AF_INET, SOCK_STREAM, 0);
  g_assert (s != INVALID_SOCKET);

  memset (&addr, 0, sizeof (addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons (5555);
  addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  
  rc = bind (s, (struct sockaddr *)&addr, sizeof (addr));
  g_assert (rc != SOCKET_ERROR);

  rc = listen (s, 1);
  g_assert (rc != SOCKET_ERROR);

  t = accept (s, NULL, NULL);
  g_assert (t != INVALID_SOCKET);


  while ((rc = recv (t, buffer, sizeof (buffer), 0)) > 0)
    g_print ("got %d bytes\n", rc);

  g_print ("got EOF\n");

  closesocket (s);
  closesocket (t);

  return 0;
}
