#include <glib.h>
#include <stdlib.h>

#ifdef __WIN32__
#include <winsock2.h>
#else
#include <netdb.h>
#endif

gboolean 
in_handler(GIOChannel *channel, GIOCondition source, gpointer data)
{
  g_printerr("IN called, good\n");

  return FALSE;
}

gboolean 
hup_handler(GIOChannel *channel, GIOCondition source, gpointer data)
{
  g_printerr("HUP called, bad\n");

  return FALSE;
}

int main(int argc, char **argv)
{
  GMainLoop *loop;
  struct hostent *he;
  struct sockaddr_in sin;
  int fd;
  GIOChannel *ioc;
  
  if (argc != 2)
    {
      g_printerr ("Usage: multiwatch MAIL_SERVER\n");
      exit (1);
    }

  he = gethostbyname(argv[1]);
  if (!he)
    {
      g_printerr ("Can't find host %s\n", argv[1]);
      exit (1);
    }

  fd = socket (PF_INET, SOCK_STREAM, 0);

  sin.sin_family = AF_INET;
  sin.sin_port = htons (25);
  sin.sin_addr = *(struct in_addr *)he->h_addr;

  if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    {
      g_printerr ("Can't connect to  host %s\n", argv[1]);
      exit (1);
    }

  ioc = g_io_channel_unix_new (fd);

  g_io_add_watch(ioc, G_IO_IN, in_handler, NULL);
  g_io_add_watch(ioc, G_IO_HUP, hup_handler, NULL);

  g_io_channel_unref (ioc);
  
  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);

  return 0;
}
