Index: cpukit/libnetworking/kern/uipc_socket.c
===================================================================
--- cpukit/libnetworking/kern/uipc_socket.c	(revision 51c88e8bf4dd7ae099c935878faae2a1b1ca81e7)
+++ cpukit/libnetworking/kern/uipc_socket.c	(revision f87ede57a2e97f0743a85b94072c7163fa485ae9)
@@ -146,4 +146,24 @@
 }
 
+static void
+rtems_socket_close_notify(struct socket *so)
+{
+	if (so->so_pgid) {
+		rtems_event_system_send(so->so_pgid, RTEMS_EVENT_SYSTEM_NETWORK_CLOSE);
+	}
+}
+
+static void
+rtems_sockbuf_close_notify(struct socket *so, struct sockbuf *sb)
+{
+	if (sb->sb_flags & SB_WAIT) {
+		rtems_event_system_send(sb->sb_sel.si_pid,
+		    RTEMS_EVENT_SYSTEM_NETWORK_CLOSE);
+	}
+
+	if (sb->sb_wakeup)
+		(*sb->sb_wakeup)(so, sb->sb_wakeuparg);
+}
+
 /*
  * Close a socket on last file table reference removal.
@@ -156,4 +176,8 @@
 	int s = splnet();		/* conservative */
 	int error = 0;
+
+	rtems_socket_close_notify(so);
+	rtems_sockbuf_close_notify(so, &so->so_snd);
+	rtems_sockbuf_close_notify(so, &so->so_rcv);
 
 	if (so->so_options & SO_ACCEPTCONN) {
@@ -743,5 +767,6 @@
 			error = sbwait(&so->so_rcv);
 			if (error) {
-				sbunlock(&so->so_rcv);
+				if (error != ENXIO)
+					sbunlock(&so->so_rcv);
 				splx(s);
 				return (0);
Index: cpukit/libnetworking/rtems/rtems_bsdnet_internal.h
===================================================================
--- cpukit/libnetworking/rtems/rtems_bsdnet_internal.h	(revision 51c88e8bf4dd7ae099c935878faae2a1b1ca81e7)
+++ cpukit/libnetworking/rtems/rtems_bsdnet_internal.h	(revision f87ede57a2e97f0743a85b94072c7163fa485ae9)
@@ -220,5 +220,5 @@
 #define NETISR_ARP_EVENT       (1L << NETISR_ARP)
 #define NETISR_EVENTS  (NETISR_IP_EVENT|NETISR_ARP_EVENT)
-#if (SBWAIT_EVENT & SOSLEEP_EVENT & NETISR_EVENTS)
+#if (SBWAIT_EVENT & SOSLEEP_EVENT & NETISR_EVENTS & RTEMS_EVENT_SYSTEM_NETWORK_CLOSE)
 # error "Network event conflict"
 #endif
Index: cpukit/libnetworking/rtems/rtems_glue.c
===================================================================
--- cpukit/libnetworking/rtems/rtems_glue.c	(revision 51c88e8bf4dd7ae099c935878faae2a1b1ca81e7)
+++ cpukit/libnetworking/rtems/rtems_glue.c	(revision f87ede57a2e97f0743a85b94072c7163fa485ae9)
@@ -433,4 +433,43 @@
 }
 
+static int
+rtems_bsdnet_sleep(rtems_event_set in, rtems_interval ticks)
+{
+	rtems_status_code sc;
+	rtems_event_set out;
+	rtems_event_set out2;
+
+	in |= RTEMS_EVENT_SYSTEM_NETWORK_CLOSE;
+
+	/*
+	 * Soak up any pending events.  The sleep/wakeup synchronization in the
+	 * FreeBSD kernel has no memory.
+	 */
+	rtems_event_system_receive(in, RTEMS_EVENT_ANY | RTEMS_NO_WAIT,
+	    RTEMS_NO_TIMEOUT, &out);
+
+	/*
+	 * Wait for the wakeup event.
+	 */
+	sc = rtems_bsdnet_event_receive(in, RTEMS_EVENT_ANY | RTEMS_WAIT,
+	    ticks, &out);
+
+	/*
+	 * Get additional events that may have been received between the
+	 * rtems_event_system_receive() and the rtems_bsdnet_semaphore_obtain().
+	 */
+	rtems_event_system_receive(in, RTEMS_EVENT_ANY | RTEMS_NO_WAIT,
+	    RTEMS_NO_TIMEOUT, &out2);
+	out |= out2;
+
+	if (out & RTEMS_EVENT_SYSTEM_NETWORK_CLOSE)
+		return (ENXIO);
+
+	if (sc == RTEMS_SUCCESSFUL)
+		return (0);
+
+	return (EWOULDBLOCK);
+}
+
 /*
  * Wait for something to happen to a socket buffer
@@ -439,20 +478,10 @@
 sbwait(struct sockbuf *sb)
 {
-	rtems_event_set events;
-	rtems_id tid;
-	rtems_status_code sc;
-
-	/*
-	 * Soak up any pending events.
-	 * The sleep/wakeup synchronization in the FreeBSD
-	 * kernel has no memory.
-	 */
-	rtems_event_system_receive (SBWAIT_EVENT, RTEMS_EVENT_ANY | RTEMS_NO_WAIT, RTEMS_NO_TIMEOUT, &events);
+	int error;
 
 	/*
 	 * Set this task as the target of the wakeup operation.
 	 */
-	rtems_task_ident (RTEMS_SELF, 0, &tid);
-	sb->sb_sel.si_pid = tid;
+	sb->sb_sel.si_pid = rtems_task_self();
 
 	/*
@@ -461,17 +490,9 @@
 	sb->sb_flags |= SB_WAIT;
 
-	/*
-	 * Wait for the wakeup event.
-	 */
-	sc = rtems_bsdnet_event_receive (SBWAIT_EVENT, RTEMS_EVENT_ANY | RTEMS_WAIT, sb->sb_timeo, &events);
-
-	/*
-	 * Return the status of the wait.
-	 */
-	switch (sc) {
-	case RTEMS_SUCCESSFUL:	return 0;
-	case RTEMS_TIMEOUT:	return EWOULDBLOCK;
-	default:		return ENXIO;
-	}
+	error = rtems_bsdnet_sleep(SBWAIT_EVENT, sb->sb_timeo);
+	if (error != ENXIO)
+		sb->sb_flags &= ~SB_WAIT;
+
+	return (error);
 }
 
@@ -486,5 +507,4 @@
 {
 	if (sb->sb_flags & SB_WAIT) {
-		sb->sb_flags &= ~SB_WAIT;
 		rtems_event_system_send (sb->sb_sel.si_pid, SBWAIT_EVENT);
 	}
@@ -515,14 +535,5 @@
 soconnsleep (struct socket *so)
 {
-	rtems_event_set events;
-	rtems_id tid;
-	rtems_status_code sc;
-
-	/*
-	 * Soak up any pending events.
-	 * The sleep/wakeup synchronization in the FreeBSD
-	 * kernel has no memory.
-	 */
-	rtems_event_system_receive (SOSLEEP_EVENT, RTEMS_EVENT_ANY | RTEMS_NO_WAIT, RTEMS_NO_TIMEOUT, &events);
+	int error;
 
 	/*
@@ -531,22 +542,11 @@
 	if (so->so_pgid)
 		rtems_panic ("Another task is already sleeping on that socket");
-	rtems_task_ident (RTEMS_SELF, 0, &tid);
-	so->so_pgid = tid;
-
-	/*
-	 * Wait for the wakeup event.
-	 */
-	sc = rtems_bsdnet_event_receive (SOSLEEP_EVENT, RTEMS_EVENT_ANY | RTEMS_WAIT, so->so_rcv.sb_timeo, &events);
-
-	/*
-	 * Relinquish ownership of the socket.
-	 */
-	so->so_pgid = 0;
-
-	switch (sc) {
-	case RTEMS_SUCCESSFUL:	return 0;
-	case RTEMS_TIMEOUT:	return EWOULDBLOCK;
-	default:		return ENXIO;
-	}
+	so->so_pgid = rtems_task_self();
+
+	error = rtems_bsdnet_sleep(SOSLEEP_EVENT, so->so_rcv.sb_timeo);
+	if (error != ENXIO)
+		so->so_pgid = 0;
+
+	return (error);
 }
 
Index: cpukit/libnetworking/rtems/rtems_select.c
===================================================================
--- cpukit/libnetworking/rtems/rtems_select.c	(revision 51c88e8bf4dd7ae099c935878faae2a1b1ca81e7)
+++ cpukit/libnetworking/rtems/rtems_select.c	(revision f87ede57a2e97f0743a85b94072c7163fa485ae9)
@@ -120,5 +120,6 @@
 	rtems_id tid;
 	rtems_interval then = 0, now;
-	rtems_event_set events;
+	rtems_event_set in = SBWAIT_EVENT | RTEMS_EVENT_SYSTEM_NETWORK_CLOSE;
+	rtems_event_set out;
 
 	if (nfds < 0)
@@ -146,5 +147,5 @@
 
 	rtems_task_ident (RTEMS_SELF, 0, &tid);
-	rtems_event_system_receive (SBWAIT_EVENT, RTEMS_EVENT_ANY | RTEMS_NO_WAIT, RTEMS_NO_TIMEOUT, &events);
+	rtems_event_system_receive (in, RTEMS_EVENT_ANY | RTEMS_NO_WAIT, RTEMS_NO_TIMEOUT, &out);
 	for (;;) {
 		rtems_bsdnet_semaphore_obtain ();
@@ -160,5 +161,5 @@
 			then = now;
 		}
-		rtems_event_system_receive (SBWAIT_EVENT, RTEMS_EVENT_ANY | RTEMS_WAIT, timo, &events);
+		rtems_event_system_receive (in, RTEMS_EVENT_ANY | RTEMS_WAIT, timo, &out);
 	}
 
Index: cpukit/libnetworking/rtems/rtems_syscall.c
===================================================================
--- cpukit/libnetworking/rtems/rtems_syscall.c	(revision 51c88e8bf4dd7ae099c935878faae2a1b1ca81e7)
+++ cpukit/libnetworking/rtems/rtems_syscall.c	(revision f87ede57a2e97f0743a85b94072c7163fa485ae9)
@@ -205,15 +205,15 @@
 		return -1;
 	}
-	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
+	error = so->so_error;
+	while (error == 0 && (so->so_state & SS_ISCONNECTING)) {
 		error = soconnsleep (so);
 		if (error)
 			break;
-	}
-	if (error == 0) {
 		error = so->so_error;
 		so->so_error = 0;
 	}
     bad:
-	so->so_state &= ~SS_ISCONNECTING;
+	if (error != ENXIO)
+		so->so_state &= ~SS_ISCONNECTING;
 	m_freem (nam);
 	if (error)
@@ -250,4 +250,5 @@
 	struct socket *head, *so;
 	struct mbuf *nam;
+	int error;
 
 	rtems_bsdnet_semaphore_obtain ();
@@ -266,14 +267,14 @@
 		return -1;
 	}
-        while (head->so_comp.tqh_first == NULL && head->so_error == 0) {
+	error = head->so_error;
+        while (error ==  0 && head->so_comp.tqh_first == NULL) {
                 if (head->so_state & SS_CANTRCVMORE) {
-                        head->so_error = ECONNABORTED;
+                        error = ECONNABORTED;
                         break;
                 }
-		head->so_error = soconnsleep (head);
+		error = soconnsleep (head);
         }
-	if (head->so_error) {
-		errno = head->so_error;
-		head->so_error = 0;
+	if (error) {
+		errno = error;
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -716,4 +717,5 @@
 		return -1;
 	}
+	iop->data1 = NULL;
 	error = soclose (so);
 	rtems_bsdnet_semaphore_release ();
Index: cpukit/rtems/include/rtems/rtems/event.h
===================================================================
--- cpukit/rtems/include/rtems/rtems/event.h	(revision 51c88e8bf4dd7ae099c935878faae2a1b1ca81e7)
+++ cpukit/rtems/include/rtems/rtems/event.h	(revision f87ede57a2e97f0743a85b94072c7163fa485ae9)
@@ -315,4 +315,9 @@
 
 /**
+ * @brief Reserved system event for network socket close.
+ */
+#define RTEMS_EVENT_SYSTEM_NETWORK_CLOSE RTEMS_EVENT_26
+
+/**
  * @brief Reserved system event for transient usage.
  */
Index: testsuites/libtests/syscall01/init.c
===================================================================
--- testsuites/libtests/syscall01/init.c	(revision 51c88e8bf4dd7ae099c935878faae2a1b1ca81e7)
+++ testsuites/libtests/syscall01/init.c	(revision f87ede57a2e97f0743a85b94072c7163fa485ae9)
@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
+ * Copyright (c) 2012-2015 embedded brains GmbH.  All rights reserved.
  *
  *  embedded brains GmbH
- *  Obere Lagerstr. 30
+ *  Dornierstr. 4
  *  82178 Puchheim
  *  Germany
@@ -19,6 +19,8 @@
 #include "tmacros.h"
 
+#include <sys/select.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <netinet/in.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -30,12 +32,17 @@
 const char rtems_test_name[] = "SYSCALL 1";
 
-/* forward declarations to avoid warnings */
-static rtems_task Init(rtems_task_argument argument);
-
 static const char open_driver_path [] = "/dev/open_driver";
 
 struct rtems_bsdnet_config rtems_bsdnet_config;
 
-static void test(void)
+typedef struct {
+  rtems_id main_task;
+  rtems_id close_task;
+  int fd;
+} test_context;
+
+static test_context test_instance;
+
+static void test_sync(void)
 {
   int rv;
@@ -77,14 +84,182 @@
 }
 
+static void close_task(rtems_task_argument arg)
+{
+  test_context *ctx = (test_context *) arg;
+
+  while (true) {
+    rtems_status_code sc;
+    int rv;
+
+    rv = close(ctx->fd);
+    rtems_test_assert(rv == 0);
+
+    sc = rtems_event_transient_send(ctx->main_task);
+    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
+  }
+}
+
+static void wait_for_close_task(void)
+{
+  rtems_status_code sc;
+
+  sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
+  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
+}
+
+static void test_accept_and_close(test_context *ctx)
+{
+  int rv;
+  int fd;
+  struct sockaddr_in addr;
+  socklen_t addrlen = sizeof(addr);
+
+  ctx->fd = socket(PF_INET, SOCK_STREAM, 0);
+  rtems_test_assert(ctx->fd >= 0);
+
+  rv = listen(ctx->fd, 1);
+  rtems_test_assert(rv == 0);
+
+  errno = 0;
+  fd = accept(ctx->fd, (struct sockaddr *) &addr, &addrlen);
+  rtems_test_assert(fd == -1);
+  rtems_test_assert(errno == ENXIO);
+
+  errno = 0;
+  fd = accept(ctx->fd, (struct sockaddr *) &addr, &addrlen);
+  rtems_test_assert(fd == -1);
+  rtems_test_assert(errno == EBADF);
+
+  wait_for_close_task();
+}
+
+static void test_connect_and_close(test_context *ctx)
+{
+  int rv;
+  struct sockaddr_in addr;
+  socklen_t addrlen = sizeof(addr);
+
+  ctx->fd = socket(PF_INET, SOCK_STREAM, 0);
+  rtems_test_assert(ctx->fd >= 0);
+
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_port = htons(1234);
+  addr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+  errno = 0;
+  rv = connect(ctx->fd, (struct sockaddr *) &addr, addrlen);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == ENXIO);
+
+  errno = 0;
+  rv = connect(ctx->fd, (struct sockaddr *) &addr, addrlen);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == EBADF);
+
+  wait_for_close_task();
+}
+
+static void test_recv_and_close(test_context *ctx)
+{
+  int rv;
+  struct sockaddr_in addr;
+  socklen_t addrlen = sizeof(addr);
+  char buf[1];
+  ssize_t n;
+
+  ctx->fd = socket(PF_INET, SOCK_DGRAM, 0);
+  rtems_test_assert(ctx->fd >= 0);
+
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_port = htons(1234);
+  addr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+  rv = bind(ctx->fd, (struct sockaddr *) &addr, addrlen);
+  rtems_test_assert(rv == 0);
+
+  errno = 0;
+  n = recv(ctx->fd, &buf[0], sizeof(buf), 0);
+  rtems_test_assert(n == -1);
+  rtems_test_assert(errno == ENXIO);
+
+  errno = 0;
+  n = recv(ctx->fd, &buf[0], sizeof(buf), 0);
+  rtems_test_assert(n == -1);
+  rtems_test_assert(errno == EBADF);
+
+  wait_for_close_task();
+}
+
+static void test_select_and_close(test_context *ctx)
+{
+  int rv;
+  struct sockaddr_in addr;
+  socklen_t addrlen = sizeof(addr);
+  int nfds;
+  struct fd_set set;
+
+  ctx->fd = socket(PF_INET, SOCK_DGRAM, 0);
+  rtems_test_assert(ctx->fd >= 0);
+
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = AF_INET;
+  addr.sin_port = htons(1234);
+  addr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+  rv = bind(ctx->fd, (struct sockaddr *) &addr, addrlen);
+  rtems_test_assert(rv == 0);
+
+  nfds = ctx->fd + 1;
+  FD_ZERO(&set);
+  FD_SET(ctx->fd, &set);
+
+  errno = 0;
+  rv = select(nfds, &set, NULL, NULL, NULL);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == EBADF);
+
+  wait_for_close_task();
+}
+
 static void Init(rtems_task_argument arg)
 {
+  test_context *ctx = &test_instance;
+  rtems_status_code sc;
   int rv;
 
   TEST_BEGIN();
 
+  ctx->main_task = rtems_task_self();
+
+  sc = rtems_task_create(
+    rtems_build_name('C', 'L', 'O', 'S'),
+    2,
+    RTEMS_MINIMUM_STACK_SIZE,
+    RTEMS_DEFAULT_MODES,
+    RTEMS_DEFAULT_ATTRIBUTES,
+    &ctx->close_task
+  );
+  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
+
+  sc = rtems_task_start(
+    ctx->close_task,
+    close_task,
+    (rtems_task_argument) ctx
+  );
+  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
+
   rv = rtems_bsdnet_initialize_network();
   rtems_test_assert(rv == 0);
 
-  test();
+  test_sync();
+  test_accept_and_close(ctx);
+  test_connect_and_close(ctx);
+  test_recv_and_close(ctx);
+  test_select_and_close(ctx);
+
+  sc = rtems_task_delete(ctx->close_task);
+  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
 
   TEST_END();
@@ -130,5 +305,5 @@
 #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
 
-#define CONFIGURE_MAXIMUM_TASKS 2
+#define CONFIGURE_MAXIMUM_TASKS 3
 
 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
Index: testsuites/samples/loopback/init.c
===================================================================
--- testsuites/samples/loopback/init.c	(revision 51c88e8bf4dd7ae099c935878faae2a1b1ca81e7)
+++ testsuites/samples/loopback/init.c	(revision f87ede57a2e97f0743a85b94072c7163fa485ae9)
@@ -168,5 +168,8 @@
         s1 = accept(s, (struct sockaddr *)&farAddr, &addrlen);
         if (s1 < 0)
-            rtems_panic("Can't accept connection: %s", strerror(errno));
+            if (errno == ENXIO)
+                rtems_task_delete(RTEMS_SELF);
+            else
+                rtems_panic("Can't accept connection: %s", strerror(errno));
         else
             printf("ACCEPTED:%lX\n", ntohl(farAddr.sin_addr.s_addr));
