fixes #666 Unix domain socket files not unlinked when closed
diff --git a/AUTHORS b/AUTHORS
index f27a6b2..de20ee1 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -20,6 +20,7 @@
 Emeric Chevalier <emericchevalier.pro@gmail.com>
 Emil Renner Berthing <esmil@mailme.dk>
 Evan Wies <evan@neomantra.net>
+Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 Gareth Roberts <blutack@gmail.com>
 Garrett D'Amore <garrett@damore.org>
 Gonzalo Diethelm <gonzalo.diethelm@diethelm.org>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a16976a..3db3d11 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -181,6 +181,10 @@
 
 nn_check_struct_member(msghdr msg_control sys/socket.h NN_HAVE_MSG_CONTROL)
 
+# Windows defines AF_UNIX in winsock2.h rather than sys/socket.h, so
+# we don't need to blacklist windows in the check.
+nn_check_sym (AF_UNIX sys/socket.h NN_HAVE_UNIX_SOCKETS)
+
 if (NN_HAVE_SEMAPHORE_RT OR NN_HAVE_SEMAPHORE_PTHREAD)
     add_definitions (-DNN_HAVE_SEMAPHORE)
 endif ()
diff --git a/src/transports/ipc/bipc.c b/src/transports/ipc/bipc.c
index 8c544c9..e796f95 100644
--- a/src/transports/ipc/bipc.c
+++ b/src/transports/ipc/bipc.c
@@ -1,5 +1,6 @@
 /*
     Copyright (c) 2012-2013 Martin Sustrik  All rights reserved.
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -168,6 +169,11 @@
 static void nn_bipc_shutdown (struct nn_fsm *self, int src, int type,
     void *srcptr)
 {
+#if defined NN_HAVE_UNIX_SOCKETS
+    const char *addr;
+    int rc;
+#endif
+
     struct nn_bipc *bipc;
     struct nn_list_item *it;
     struct nn_aipc *aipc;
@@ -190,6 +196,14 @@
         nn_aipc_term (bipc->aipc);
         nn_free (bipc->aipc);
         bipc->aipc = NULL;
+
+        /* On *nixes, unlink the domain socket file */
+#if defined NN_HAVE_UNIX_SOCKETS
+        addr = nn_epbase_getaddr (&bipc->epbase);
+        rc = unlink(addr);
+        errno_assert (rc == 0 || errno == ENOENT);
+#endif
+
         nn_usock_stop (&bipc->usock);
         bipc->state = NN_BIPC_STATE_STOPPING_USOCK;
     }
@@ -383,7 +397,7 @@
     struct sockaddr_storage ss;
     struct sockaddr_un *un;
     const char *addr;
-#if !defined NN_HAVE_WINDOWS
+#if defined NN_HAVE_UNIX_SOCKETS
     int fd;
 #endif
 
@@ -399,7 +413,7 @@
         the application. We'll check whether the file is still in use by
         connecting to the endpoint. On Windows plaform, NamedPipe is used
         which does not have an underlying file. */
-#if !defined NN_HAVE_WINDOWS
+#if defined NN_HAVE_UNIX_SOCKETS
     fd = socket (AF_UNIX, SOCK_STREAM, 0);
     if (fd >= 0) {
         rc = fcntl (fd, F_SETFL, O_NONBLOCK);