diff --git a/apps/sshtosnmp.c b/apps/sshtosnmp.c
index 7b082b0..6b3a5ad 100644
--- a/apps/sshtosnmp.c
+++ b/apps/sshtosnmp.c
@@ -21,11 +21,13 @@
 #include <sys/un.h>
 #endif
 
+//XXX #include <sys/types.h>  //FIXME: needed for sendmsg?
 #include <sys/select.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <stdio.h>
+#include <string.h> // FIXME: use strcpy, memset, ...
 
 #ifndef MAXPATHLEN
 #warn no system max path length detected
@@ -84,11 +86,14 @@ main(int argc, char **argv) {
     }
 
     sock = socket(PF_UNIX, SOCK_STREAM, 0);
-    DEBUG("created socket");
-    if (sock <= 0) {
+    if (sock < 0) {
+        DEBUG("FAIL SOCKET");
+        DEBUG(strerror(errno));
         exit(1);
     }
+    DEBUG("created socket");
 
+#ifndef darwin9
     /* set the SO_PASSCRED option so we can pass uid */
     /* XXX: according to the unix(1) manual this shouldn't be needed
        on the sending side? */
@@ -97,10 +102,13 @@ main(int argc, char **argv) {
         setsockopt(sock, SOL_SOCKET, SO_PASSCRED, (void *) &one,
                    sizeof(one));
     }
+#endif
 
+    DEBUG("connect socket");
     if (connect(sock, (struct sockaddr *) &addr,
                 sizeof(struct sockaddr_un)) != 0) {
-        DEBUG("FAIL CONNECT");
+        DEBUG("FAIL CONNECT on: " DEFAULT_SOCK_PATH);
+        DEBUG(strerror(errno));
         exit(1);
     }
 
@@ -121,13 +129,15 @@ main(int argc, char **argv) {
 
     buf[0] = NETSNMP_SSHTOSNMP_VERSION_NUMBER;
     buf_len = 1;
-    
+
+#ifndef darwin9
+    DEBUG("sent name");
     /* send the prelim message and the credentials together using sendmsg() */
     {
         struct msghdr m;
         struct {
            struct cmsghdr cm;
-           struct ucred ouruser;
+           struct ucred ouruser;    //FIXME error on Darwin: field 'ouruser' has incomplete type
         } cmsg;
         struct iovec iov = { buf, buf_len };
 
@@ -138,6 +148,7 @@ main(int argc, char **argv) {
         /* set up the basic message */
         cmsg.cm.cmsg_len = sizeof(struct cmsghdr) + sizeof(struct ucred);
         cmsg.cm.cmsg_level = SOL_SOCKET;
+        //FIXME error on Darwin: 'SCM_CREDENTIALS' undeclared
         cmsg.cm.cmsg_type = SCM_CREDENTIALS;
 
         cmsg.ouruser.uid = getuid();
@@ -149,42 +160,59 @@ main(int argc, char **argv) {
         m.msg_control           = &cmsg;
         m.msg_controllen        = sizeof(cmsg);
         m.msg_flags             = 0;
-        
+
         DEBUG("sending to sock");
+        //FIXME error on Darwin: 'MSG_NOSIGNAL' undeclared
         rc = sendmsg(sock, &m, MSG_NOSIGNAL|MSG_DONTWAIT);
         if (rc < 0) {
             fprintf(stderr, "failed to send startup message\n");
             DEBUG("failed to send startup message\n");
+            DEBUG(strerror(errno));
             exit(1);
         }
     }
+#endif
 
-    DEBUG("sent name");
-    
     /* now we just send and receive from both the socket and stdin/stdout */
+    DEBUG("while work");
 
     while(1) {
         /* read from stdin and the socket */
+        FD_ZERO(&read_set);
         FD_SET(sock, &read_set);
         FD_SET(STDIN_FILENO, &read_set);
 
-        /* blocking without a timeout be fine fine */
-        select(sock+1, &read_set, NULL, NULL, NULL);
+        /* blocking without a timeout will be fine */
+        rc = select(sock+1, &read_set, NULL, NULL, NULL);
+        if (rc < 0) {
+            DEBUG("failed select\n");
+            DEBUG(strerror(errno));
+            continue;
+        }
 
         if (FD_ISSET(STDIN_FILENO, &read_set)) {
             /* read from stdin to get stuff from sshd to send to the agent */
             DEBUG("data from stdin");
             rc = read(STDIN_FILENO, buf, sizeof(buf));
 
-            if (rc <= 0) {
+            //Note: exit when read 0 bytes to terminate after EOF! ck
+            if (rc <= 0 && errno != EINTR) {
+                if (rc < 0) {
+                    DEBUG("failed read");
+                    DEBUG(strerror(errno));
+                }
                 /* end-of-file */
+
 #ifndef HAVE_CLOSESOCKET
                 rc = close(sock);
 #else
                 rc = closesocket(sock);
 #endif
+
                 exit(0);
             }
+
+
             DEBUG("read from stdin");
 
             /* send it up the pipe */
@@ -197,6 +225,7 @@ main(int argc, char **argv) {
                 if (rc < 0)
                     DEBUG("sentto failed");
                 if (rc < 0 && errno != EINTR) {
+                    DEBUG(strerror(errno));
                     break;
                 }
             }
@@ -213,20 +242,27 @@ main(int argc, char **argv) {
             rc = -1;
             while (rc < 0) {
                 rc = recvfrom(sock, buf, sizeof(buf), 0, NULL, NULL);
+                //FIXME: exit when read 0 bytes? ck
                 if (rc < 0 && errno != EINTR) {
+                    DEBUG("failed recvfrom");
+                    DEBUG(strerror(errno));
                     close(sock);
                     exit(0);
                 }
             }
-            DEBUG("read from socket");
 
-            pktsize = rc;
-            rc = write(STDOUT_FILENO, buf, pktsize);
-            /* XXX: check that counts match */
-            if (rc > 0) {
-                DEBUG("wrote to stdout");
-            } else {
-                DEBUG("failed to write to stdout");
+            // FIXME: send 0 byte message too? ck
+            if (rc >= 0) {
+                DEBUG("read from socket");
+                pktsize = rc;
+                rc = write(STDOUT_FILENO, buf, pktsize);
+                /* XXX: check that counts match */
+                if (rc >= 0 && rc == pktsize) {
+                    DEBUG("wrote to stdout");
+                } else {
+                    DEBUG("failed to write to stdout");
+                    DEBUG(strerror(errno));
+                }
             }
         }
     }
diff --git a/snmplib/transports/snmpSSHDomain.c b/snmplib/transports/snmpSSHDomain.c
index 72cc59e..77d8eff 100644
--- a/snmplib/transports/snmpSSHDomain.c
+++ b/snmplib/transports/snmpSSHDomain.c
@@ -95,7 +95,7 @@ netsnmp_sockaddr_in2(struct sockaddr_in *addr,
 
 /*
  * Return a string representing the address in data, or else the "far end"
- * address if data is NULL.  
+ * address if data is NULL.
  */
 
 static char *
@@ -104,16 +104,16 @@ netsnmp_ssh_fmtaddr(netsnmp_transport *t, void *data, int len)
     netsnmp_ssh_addr_pair *addr_pair = NULL;
 
     if (data != NULL && len == sizeof(netsnmp_ssh_addr_pair)) {
-	addr_pair = (netsnmp_ssh_addr_pair *) data;
+        addr_pair = (netsnmp_ssh_addr_pair *) data;
     } else if (t != NULL && t->data != NULL) {
-	addr_pair = (netsnmp_ssh_addr_pair *) t->data;
+        addr_pair = (netsnmp_ssh_addr_pair *) t->data;
     }
 
     if (addr_pair == NULL) {
         return strdup("SSH: unknown");
     } else {
         struct sockaddr_in *to = NULL;
-	char tmp[64];
+        char tmp[64];
         to = (struct sockaddr_in *) &(addr_pair->remote_addr);
         if (to == NULL) {
             return strdup("SSH: unknown");
@@ -128,14 +128,14 @@ netsnmp_ssh_fmtaddr(netsnmp_transport *t, void *data, int len)
 
 
 /*
- * You can write something into opaque that will subsequently get passed back 
+ * You can write something into opaque that will subsequently get passed back
  * to your send function if you like.  For instance, you might want to
- * remember where a PDU came from, so that you can send a reply there...  
+ * remember where a PDU came from, so that you can send a reply there...
  */
 
 static int
 netsnmp_ssh_recv(netsnmp_transport *t, void *buf, int size,
-		 void **opaque, int *olength)
+                 void **opaque, int *olength)
 {
     int rc = -1;
     netsnmp_tmStateReference *tmStateRef = NULL;
@@ -145,7 +145,7 @@ netsnmp_ssh_recv(netsnmp_transport *t, void *buf, int size,
     DEBUGMSGTL(("ssh", "at the top of ssh_recv\n"));
     DEBUGMSGTL(("ssh", "t=%p\n", t));
     if (t != NULL && t->data != NULL) {
-	addr_pair = (netsnmp_ssh_addr_pair *) t->data;
+        addr_pair = (netsnmp_ssh_addr_pair *) t->data;
     }
 
     DEBUGMSGTL(("ssh", "addr_pair=%p\n", addr_pair));
@@ -153,16 +153,16 @@ netsnmp_ssh_recv(netsnmp_transport *t, void *buf, int size,
         DEBUGMSGTL(("ssh", "t=%p, addr_pair=%p, channel=%p\n",
                     t, addr_pair, addr_pair->channel));
         iamclient = 1;
-	while (rc < 0) {
-	    rc = libssh2_channel_read(addr_pair->channel, buf, size);
-	    if (rc < 0) {  /* XXX: from tcp; ssh equiv?:  && errno != EINTR */
-		DEBUGMSGTL(("ssh", "recv fd %d err %d (\"%s\")\n",
-			    t->sock, errno, strerror(errno)));
-		break;
-	    }
-	    DEBUGMSGTL(("ssh", "recv fd %d got %d bytes\n",
-			t->sock, rc));
-	}
+        while (rc < 0) {
+            rc = libssh2_channel_read(addr_pair->channel, buf, size);
+            if (rc < 0) {  /* XXX: from tcp; ssh equiv?:  && errno != EINTR */
+                DEBUGMSGTL(("ssh", "recv fd %d err %d (\"%s\")\n",
+                            t->sock, errno, strerror(errno)));
+                break;
+            }
+            DEBUGMSGTL(("ssh", "recv fd %d got %d bytes\n",
+                        t->sock, rc));
+        }
     } else if (t != NULL) {
 
 #ifdef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
@@ -185,6 +185,7 @@ netsnmp_ssh_recv(netsnmp_transport *t, void *buf, int size,
                 return -1;
             };
 
+#ifndef darwin9
             if (addr_pair->username[0] == '\0') {
                 /* we don't have a username yet, so this is the first message */
                 struct ucred *remoteuser;
@@ -202,7 +203,7 @@ netsnmp_ssh_recv(netsnmp_transport *t, void *buf, int size,
                 msg.msg_iovlen = 1;
                 msg.msg_control = &cmsg;
                 msg.msg_controllen = sizeof(cmsg);
-                
+
                 rc = recvmsg(t->sock, &msg, MSG_DONTWAIT); /* use DONTWAIT? */
                 if (rc <= 0) {
                     return rc;
@@ -258,7 +259,13 @@ netsnmp_ssh_recv(netsnmp_transport *t, void *buf, int size,
 
                 rc -= 1;
                 memmove(charbuf, &charbuf[1], rc);
-            } else {
+
+            } else
+
+#endif  // darwin9
+
+            {
+
                 while (rc < 0) {
                     rc = recvfrom(t->sock, buf, size, 0, NULL, NULL);
                     if (rc < 0 && errno != EINTR) {
@@ -273,7 +280,7 @@ netsnmp_ssh_recv(netsnmp_transport *t, void *buf, int size,
             DEBUGMSGTL(("ssh", "recv fd %d got %d bytes\n",
                         t->sock, rc));
         }
-        
+
 #else /* we're called directly by sshd and use stdin/out */
 
         struct passwd *user_pw;
@@ -334,13 +341,32 @@ netsnmp_ssh_recv(netsnmp_transport *t, void *buf, int size,
                 sizeof(tmStateRef->securityName)-1);
     } else {
 #ifdef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
-        strncpy(tmStateRef->securityName, addr_pair->username,
-                sizeof(tmStateRef->securityName)-1);
+        if (addr_pair->username[0] != '\0') {
+            strncpy(tmStateRef->securityName, addr_pair->username,
+                    sizeof(tmStateRef->securityName)-1);
+
+#ifdef darwin9
+        } else {
+            /* 
+             * FIXME: This is a pour hack to test on MAC-OX:
+             */
+#warning "Every ssh user will be mapped to the configured NETSNMP_DS_LIB_SSH_USERNAME!"
+
+            //XXX DEBUGMSGTL(("ssh:getenv", "current username=%s\n", getenv("USER"))); // always root!
+            char * username = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
+                                         NETSNMP_DS_LIB_SSH_USERNAME);
+            if (username && 0 != *username) {
+                strncpy(tmStateRef->securityName, username,
+                        sizeof(tmStateRef->securityName)-1);
+            }
+#endif // darwin9
+
+        }
 #else /* we're called directly by sshd and use stdin/out */
         /* we're on the server... */
         /* XXX: this doesn't copy properly and can get pointer
            reference issues */
-        if (strlen(getenv("USER")) > 127) {
+        if (strlen(getenv("USER")) > MAX_NAME_LENGTH) {
             /* ruh roh */
             /* XXX: clean up */
             return -1;
@@ -355,6 +381,7 @@ netsnmp_ssh_recv(netsnmp_transport *t, void *buf, int size,
     }
     tmStateRef->securityName[sizeof(tmStateRef->securityName)-1] = '\0';
     tmStateRef->securityNameLen = strlen(tmStateRef->securityName);
+    DEBUGMSGTL(("ssh", "current username=%s\n", tmStateRef->securityName));
     *opaque = tmStateRef;
     *olength = sizeof(netsnmp_tmStateReference);
 
@@ -365,7 +392,7 @@ netsnmp_ssh_recv(netsnmp_transport *t, void *buf, int size,
 
 static int
 netsnmp_ssh_send(netsnmp_transport *t, void *buf, int size,
-		 void **opaque, int *olength)
+                 void **opaque, int *olength)
 {
     int rc = -1;
 
@@ -373,7 +400,7 @@ netsnmp_ssh_send(netsnmp_transport *t, void *buf, int size,
     netsnmp_tmStateReference *tmStateRef = NULL;
 
     if (t != NULL && t->data != NULL) {
-	addr_pair = (netsnmp_ssh_addr_pair *) t->data;
+        addr_pair = (netsnmp_ssh_addr_pair *) t->data;
     }
 
     if (opaque != NULL && *opaque != NULL &&
@@ -398,18 +425,18 @@ netsnmp_ssh_send(netsnmp_transport *t, void *buf, int size,
             snmp_log(LOG_ERR, "netsnmp_ssh_send was passed a tmStateReference with a securityName not equal to previous messages\n");
             return -1;
         }
-	while (rc < 0) {
-	    rc = libssh2_channel_write(addr_pair->channel, buf, size);
-	    if (rc < 0) { /* XXX:  && errno != EINTR */
-		break;
-	    }
-	}
+        while (rc < 0) {
+            rc = libssh2_channel_write(addr_pair->channel, buf, size);
+            if (rc < 0) { /* XXX:  && errno != EINTR */
+                break;
+            }
+        }
     } else if (t != NULL) {
 #ifdef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
 
-	while (rc < 0) {
+        while (rc < 0) {
             rc = sendto(t->sock, buf, size, 0, NULL, 0);
-            
+
             if (rc < 0 && errno != EINTR) {
                 break;
             }
@@ -417,13 +444,13 @@ netsnmp_ssh_send(netsnmp_transport *t, void *buf, int size,
 
 #else /* we're called directly by sshd and use stdin/out */
         /* on the server; send to stdout */
-	while (rc < 0) {
-	    rc = write(STDOUT_FILENO, buf, size);
+        while (rc < 0) {
+            rc = write(STDOUT_FILENO, buf, size);
             fflush(stdout);
-	    if (rc < 0 && errno != EINTR) { /* XXX:  && errno != EINTR */
-		break;
-	    }
-	}
+            if (rc < 0 && errno != EINTR) { /* XXX:  && errno != EINTR */
+                break;
+            }
+        }
 #endif
     }
 
@@ -439,7 +466,7 @@ netsnmp_ssh_close(netsnmp_transport *t)
     netsnmp_ssh_addr_pair *addr_pair = NULL;
 
     if (t != NULL && t->data != NULL) {
-	addr_pair = (netsnmp_ssh_addr_pair *) t->data;
+        addr_pair = (netsnmp_ssh_addr_pair *) t->data;
     }
 
     if (t != NULL && addr_pair && t->sock >= 0) {
@@ -467,7 +494,7 @@ netsnmp_ssh_close(netsnmp_transport *t)
 #ifdef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
 
         close(t->sock);
-        
+
         if (!addr_pair->session && !addr_pair->channel) {
             /* unix socket based connection */
             close(t->sock);
@@ -504,7 +531,7 @@ netsnmp_ssh_accept(netsnmp_transport *t)
 
     /* much of this is duplicated from snmpUnixDomain.c */
 
-    netsnmp_ssh_addr_pair *addr_pair;    
+    netsnmp_ssh_addr_pair *addr_pair;
     int                    newsock   = -1;
     struct sockaddr       *farend    = NULL;
     socklen_t              farendlen = sizeof(struct sockaddr_un);
@@ -525,12 +552,14 @@ netsnmp_ssh_accept(netsnmp_transport *t)
 
         newsock = accept(t->sock, farend, &farendlen);
 
+#ifndef darwin9
         /* set the SO_PASSCRED option so we can receive the remote uid */
         {
             int one = 1;
             setsockopt(newsock, SOL_SOCKET, SO_PASSCRED, (void *) &one,
                        sizeof(one));
         }
+#endif
 
         if (newsock < 0) {
             DEBUGMSGTL(("ssh","accept failed rc %d errno %d \"%s\"\n",
@@ -557,7 +586,7 @@ netsnmp_ssh_accept(netsnmp_transport *t)
 #else /* we're called directly by sshd and use stdin/out */
     /* we don't need to do anything; server side uses stdin/out */
     /* XXX: check that we're an ssh connection */
-    
+
     return STDIN_FILENO; /* return stdin */
 #endif /* ! SNMPSSHDOMAIN_USE_EXTERNAL_PIPE */
 
@@ -567,8 +596,8 @@ netsnmp_ssh_accept(netsnmp_transport *t)
 
 /*
  * Open a SSH-based transport for SNMP.  Local is TRUE if addr is the local
- * address to bind to (i.e. this is a server-type session); otherwise addr is 
- * the remote address to send things to.  
+ * address to bind to (i.e. this is a server-type session); otherwise addr is
+ * the remote address to send things to.
  */
 
 netsnmp_transport *
@@ -642,17 +671,19 @@ netsnmp_ssh_transport(struct sockaddr_in *addr, int local)
             return NULL;
         }
 
+#ifndef darwin9
         /* set the SO_PASSCRED option so we can receive the remote uid */
         {
             int one = 1;
             setsockopt(t->sock, SOL_SOCKET, SO_PASSCRED, (void *) &one,
                        sizeof(one));
         }
+#endif
 
         unlink(unaddr->sun_path);
         rc = bind(t->sock, (struct sockaddr *) unaddr, SUN_LEN(unaddr));
         if (rc != 0) {
-            DEBUGMSGTL(("netsnmp_ssh_transport",
+            DEBUGMSGTL(("ssh",
                         "couldn't bind \"%s\", errno %d (%s)\n",
                         unaddr->sun_path, errno, strerror(errno)));
             netsnmp_ssh_close(t);
@@ -705,14 +736,14 @@ netsnmp_ssh_transport(struct sockaddr_in *addr, int local)
 
         rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
         if (rc != 0) {
-            DEBUGMSGTL(("netsnmp_ssh_transport",
+            DEBUGMSGTL(("ssh",
                         "couldn't listen to \"%s\", errno %d (%s)\n",
                         unaddr->sun_path, errno, strerror(errno)));
             netsnmp_ssh_close(t);
             netsnmp_transport_free(t);
             return NULL;
         }
-        
+
 
 #else /* we're called directly by sshd and use stdin/out */
         /* for ssh on the server side we've been launched so bind to
@@ -731,7 +762,7 @@ netsnmp_ssh_transport(struct sockaddr_in *addr, int local)
         size_t username_len;
         char *keyfilepub;
         char *keyfilepriv;
-        
+
         /* use the requested user name */
         /* XXX: default to the current user name on the system like ssh does */
         username = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
@@ -760,7 +791,7 @@ netsnmp_ssh_transport(struct sockaddr_in *addr, int local)
             return NULL;
         }
 
-        /* xxx: need an ipv6 friendly one too (sigh) */
+        /* XXX: need an ipv6 friendly one too (sigh) */
 
         /* XXX: not ideal when structs don't actually match size wise */
         memcpy(&(addr_pair->remote_addr), addr, sizeof(struct sockaddr_in));
@@ -790,7 +821,7 @@ netsnmp_ssh_transport(struct sockaddr_in *addr, int local)
          */
 
         rc = connect(t->sock, (struct sockaddr *)addr,
-		     sizeof(struct sockaddr));
+                     sizeof(struct sockaddr));
 
         if (rc < 0) {
             netsnmp_ssh_close(t);
@@ -801,7 +832,7 @@ netsnmp_ssh_transport(struct sockaddr_in *addr, int local)
         /*
          * Allow user to override the send and receive buffers. Default is
          * to use os default.  Don't worry too much about errors --
-         * just plough on regardless.  
+         * just plough on regardless.
          */
         netsnmp_sock_buffer_set(t->sock, SO_SNDBUF, local, 0);
         netsnmp_sock_buffer_set(t->sock, SO_RCVBUF, local, 0);
@@ -886,7 +917,7 @@ netsnmp_ssh_transport(struct sockaddr_in *addr, int local)
     DEBUGMSG(("ssh","Opened connection.\n"));
     /*
      * Message size is not limited by this transport (hence msgMaxSize
-     * is equal to the maximum legal size of an SNMP message).  
+     * is equal to the maximum legal size of an SNMP message).
      */
 
     t->msgMaxSize = 0x7fffffff;
@@ -903,7 +934,7 @@ netsnmp_ssh_transport(struct sockaddr_in *addr, int local)
 
 netsnmp_transport *
 netsnmp_ssh_create_tstring(const char *str, int local,
-			   const char *default_target)
+                           const char *default_target)
 {
     struct sockaddr_in addr;
 
@@ -978,7 +1009,7 @@ sshdomain_parse_socket(const char *token, char *cptr)
 }
 
 void
-netsnmp_ssh_ctor(void)    
+netsnmp_ssh_ctor(void)
 {
     sshDomain.name = netsnmp_snmpSSHDomain;
     sshDomain.name_length = netsnmp_snmpSSHDomain_len;
