Index: giowin32.c
===================================================================
RCS file: /cvs/gnome/glib/glib/giowin32.c,v
retrieving revision 1.67
diff -p -u -2 -r1.67 giowin32.c
--- giowin32.c	11 Mar 2006 21:03:00 -0000	1.67
+++ giowin32.c	31 Mar 2006 22:37:23 -0000
@@ -115,4 +115,5 @@ struct _GIOWin32Channel {
   int event;
   gboolean write_would_have_blocked;
+  gboolean close_event_received;
 };
 
@@ -357,4 +358,5 @@ g_io_channel_win32_init (GIOWin32Channel
   channel->event = 0;
   channel->write_would_have_blocked = FALSE;
+  channel->close_event_received = FALSE;
   InitializeCriticalSection (&channel->mutex);
 }
@@ -692,5 +694,4 @@ buffer_read (GIOWin32Channel *channel,
 }
 
-
 static GIOStatus
 buffer_write (GIOWin32Channel *channel,
@@ -763,5 +764,4 @@ buffer_write (GIOWin32Channel *channel,
 }
 
-
 static gboolean
 g_io_win32_prepare (GSource *source,
@@ -772,4 +772,5 @@ g_io_win32_prepare (GSource *source,
   GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
   int event_mask;
+  gboolean retval;
   
   *timeout = -1;
@@ -830,11 +831,13 @@ g_io_win32_prepare (GSource *source,
 		     event_mask_to_string (event_mask));
 	  if (WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd,
-			      event_mask) == SOCKET_ERROR)
-	    ;			/* What? */
-	  channel->event_mask = event_mask;
-#if 0
-	  channel->event = watch->pollfd.fd;
-#endif
-	  channel->last_events = 0;
+			      event_mask) != SOCKET_ERROR)
+	    {
+	      channel->event_mask = event_mask;
+	      channel->last_events = 0;
+	    }
+	  else
+	    {
+	      /* error message? */
+	    }
 	}
       break;
@@ -844,5 +847,12 @@ g_io_win32_prepare (GSource *source,
       abort ();
     }
-  return ((watch->condition & buffer_condition) == watch->condition);
+
+  retval = ((watch->condition & buffer_condition) != 0 ||
+	    (channel->close_event_received && ((watch->condition & G_IO_HUP) != 0)));
+
+  if (channel->debug)
+    g_print ("g_io_win32_prepare: returning %s\n", retval ? "TRUE" : "FALSE");
+
+  return retval;
 }
 
@@ -855,4 +865,5 @@ g_io_win32_check (GSource *source)
   GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
   WSANETWORKEVENTS events;
+  gboolean retval;
 
   switch (channel->type)
@@ -872,5 +883,10 @@ g_io_win32_check (GSource *source)
       watch->pollfd.revents = (watch->pollfd.events & channel->revents);
 
-      return ((watch->pollfd.revents | buffer_condition) & watch->condition);
+      retval = ((watch->pollfd.revents | buffer_condition) & watch->condition);
+
+      if (channel->debug)
+	g_print ("g_io_win32_check: returning %s\n", retval ? "TRUE" : "FALSE");
+
+      return retval;
 
     case G_IO_WIN32_SOCKET:
@@ -894,4 +910,5 @@ g_io_win32_check (GSource *source)
 	  if (watch->pollfd.revents != 0 &&
 	      events.lNetworkEvents == 0 &&
+	      !(buffer_condition & G_IO_IN) &&
 	      !(channel->event_mask & FD_WRITE))
 	    {
@@ -913,5 +930,7 @@ g_io_win32_check (GSource *source)
       if (channel->last_events & (FD_WRITE | FD_CONNECT))
 	watch->pollfd.revents |= G_IO_OUT;
-      if (watch->pollfd.revents == 0 && (channel->last_events & (FD_CLOSE)))
+      if (channel->last_events & (FD_CLOSE))
+	channel->close_event_received = TRUE;
+      if (watch->pollfd.revents == 0 && channel->close_event_received)
 	watch->pollfd.revents |= G_IO_HUP;
 
@@ -919,5 +938,10 @@ g_io_win32_check (GSource *source)
 	watch->pollfd.revents |= G_IO_OUT; /* This sucks but... */
 
-      return ((watch->pollfd.revents | buffer_condition) & watch->condition);
+      retval = ((watch->pollfd.revents | buffer_condition) & watch->condition);
+
+      if (channel->debug)
+	g_print ("g_io_win32_check: returning %s\n", retval ? "TRUE" : "FALSE");
+
+      return retval;
 
     default:
@@ -945,5 +969,5 @@ g_io_win32_dispatch (GSource     *source
   
   if (channel->debug)
-    g_print ("g_io_win32_dispatch: pollfd.revents=%s condition=%s result=%s\n",
+    g_print ("g_io_win32_dispatch: pollfd.revents={%s} condition={%s} result={%s}\n",
 	     condition_to_string (watch->pollfd.revents),
 	     condition_to_string (watch->condition),
