Index: libroar/vio_stream.c
===================================================================
--- libroar/vio_stream.c	(revision 6026)
+++ libroar/vio_stream.c	(revision 6027)
@@ -155,23 +155,5 @@
 }
 
-static int _roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, int dir, int mixer) {
- struct roar_libroar_config * config = roar_libroar_get_config();
- char file[80] = "";
- int fh = -1, listen = -1;
- static int count = 0;
- int    port = 0;
-#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET)
- int    opt  = 1;
-#endif
-#ifdef ROAR_HAVE_LIBDNET
- struct dn_naddr      *binaddr;
-#endif
-#ifdef ROAR_HAVE_IPV4
- struct sockaddr_in   socket_addr;
- socklen_t            len            = sizeof(struct sockaddr_in);
-#else
- struct sockaddr      socket_addr;
- socklen_t            len            = sizeof(struct sockaddr);
-#endif
+static inline int _roar_simple_new_stream_obj_try_select (struct roar_connection * con, struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, int dir, int mixer, struct roar_sockname * sockname, int * fh) {
 #ifdef ROAR_HAVE_SELECT
  int confh;
@@ -179,44 +161,28 @@
  struct timeval timeout = {10, 0};
  struct roar_message    mes;
-#endif
-#ifdef ROAR_HAVE_UNIX
- int socks[2]; // for socketpair()
-#endif
- struct roar_sockname sockname;
+ int listen;
+ int    port = 0;
+ char file[80] = "";
+ static int count = 0;
  char socketaddr[80];
  ssize_t socketaddr_len = -1;
-
- // make valgrind happy
+#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET)
+ int    opt  = 1;
+#endif
+#ifdef ROAR_HAVE_LIBDNET
+ struct dn_naddr      *binaddr;
+#endif
+#ifdef ROAR_HAVE_IPV4
+ struct sockaddr_in   socket_addr;
+ socklen_t            len            = sizeof(struct sockaddr_in);
+#else
+ struct sockaddr      socket_addr;
+ socklen_t            len            = sizeof(struct sockaddr);
+#endif
+
+ memset(&mes, 0, sizeof(mes));
  memset(&socket_addr, 0, sizeof(socket_addr));
-#ifdef ROAR_HAVE_SELECT
- memset(&mes,         0, sizeof(mes));
-#endif
-
- ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
-
- if ( config != NULL ) {
-  if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) {
-   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
-  }
- }
-
- ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
-
- roar_libroar_nowarn();
- if ( roar_vio_ctl(roar_get_connection_vio2(con), ROAR_VIO_CTL_GET_SOCKNAME, &sockname) == -1 ) {
-  roar_libroar_warn();
-#ifdef ROAR_OS_OPENBSD
-  sockname.type = ROAR_SOCKET_TYPE_UNIX;
-#else
-  ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = -1", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
-
-  return -1;
-#endif
- }
- roar_libroar_warn();
-
- ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
-
- if ( sockname.type == ROAR_SOCKET_TYPE_DECNET ) {
+
+ if ( sockname->type == ROAR_SOCKET_TYPE_DECNET ) {
 #ifdef ROAR_HAVE_LIBDNET
   if ( roar_socket_get_local_nodename() != NULL && (binaddr = getnodeadd()) != NULL ) {
@@ -235,7 +201,7 @@
 #ifdef ROAR_HAVE_IPV4
  } else {
-  strncpy(file, sockname.addr, sizeof(file) - 1);
+  strncpy(file, sockname->addr, sizeof(file) - 1);
   roar_err_set(ROAR_ERROR_NONE);
-  if ( inet_aton(sockname.addr, &socket_addr.sin_addr) == 0 ) {
+  if ( inet_aton(sockname->addr, &socket_addr.sin_addr) == 0 ) {
    roar_err_update();
    return -1;
@@ -246,14 +212,12 @@
  }
 
- if ( sockname.type != ROAR_SOCKET_TYPE_UNIX ) {
-  roar_libroar_nowarn();
-  if ( (listen = roar_socket_listen(sockname.type, file, port)) == -1 ) {
-   roar_libroar_warn();
-   return -1;
-  }
+ roar_libroar_nowarn();
+ if ( (listen = roar_socket_listen(sockname->type, file, port)) == -1 ) {
   roar_libroar_warn();
- }
-
- if ( sockname.type == ROAR_SOCKET_TYPE_INET ) {
+  return -1;
+ }
+ roar_libroar_warn();
+
+ if ( sockname->type == ROAR_SOCKET_TYPE_INET ) {
 #ifdef ROAR_HAVE_IPV4
   setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int));
@@ -269,5 +233,5 @@
   return -1;
 #endif
- } else if ( sockname.type == ROAR_SOCKET_TYPE_DECNET ) {
+ } else if ( sockname->type == ROAR_SOCKET_TYPE_DECNET ) {
 #ifdef ROAR_HAVE_LIBDNET
   setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
@@ -277,89 +241,127 @@
  }
 
- if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
-  return -1;
- }
-
- if ( roar_stream_connect(con, s, dir, mixer) == -1 ) {
-  return -1;
- }
-
- if ( sockname.type != ROAR_SOCKET_TYPE_UNIX ) {
-#ifdef ROAR_HAVE_SELECT
-  if ( roar_stream_connect_to_advanced(con, s, 0, 1, 0, sockname.type, socketaddr_len, socketaddr, ROAR_PROTO_NONE, -1, NULL, 0, NULL) != -1 ) {
-
-   FD_ZERO(&fds);
-   FD_SET(listen, &fds);
-
-   confh = roar_get_connection_fh(con);
-
-   if ( confh != -1 ) {
-    FD_SET(confh, &fds);
+ if ( roar_stream_connect_to_advanced(con, s, 0, 1, 0, sockname->type, socketaddr_len, socketaddr, ROAR_PROTO_NONE, -1, NULL, 0, NULL) != -1 ) {
+
+  FD_ZERO(&fds);
+  FD_SET(listen, &fds);
+
+  confh = roar_get_connection_fh(con);
+
+  if ( confh != -1 ) {
+   FD_SET(confh, &fds);
+  }
+
+  if ( select((confh > listen ? confh : listen) + 1, &fds, NULL, NULL, &timeout) < 1 ) {
+   close(listen);
+
+   // we don't need to check the content as we know it failed...
+   if ( roar_recv_message(con, &mes, NULL) == -1 )
+    return -1;
+
+   if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
+    return -1;
+
+   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
+  }
+
+  if ( FD_ISSET(listen, &fds) ) {
+   if ( (*fh = accept(listen, NULL, NULL)) != -1 ) {
+    /* TODO: FIXME: XXX: errr, do we need any error handling here? */
    }
 
-   if ( select((confh > listen ? confh : listen) + 1, &fds, NULL, NULL, &timeout) < 1 ) {
+   if ( roar_recv_message(con, &mes, NULL) == -1 ) {
+    if ( *fh != -1 )
+     close(*fh);
+    *fh = -1;
+   } else if ( mes.cmd != ROAR_CMD_OK ) {
+    if ( *fh != -1 )
+     close(*fh);
+    *fh = -1;
+   }
+  } else {
+   // we don't need to check the content as we know it failed...
+   if ( roar_recv_message(con, &mes, NULL) == -1 ) {
     close(listen);
-
-    // we don't need to check the content as we know it failed...
-    if ( roar_recv_message(con, &mes, NULL) == -1 )
-     return -1;
-
+    return -1;
+   }
+
+   if ( mes.cmd != ROAR_CMD_OK ) {
+    close(listen);
     if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
      return -1;
 
     return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
-   }
-
-   if ( FD_ISSET(listen, &fds) ) {
-    if ( (fh = accept(listen, NULL, NULL)) != -1 ) {
-     /* TODO: FIXME: XXX: errr, do we need any error handling here? */
-    }
-
-    if ( roar_recv_message(con, &mes, NULL) == -1 ) {
-     if ( fh != -1 )
-      close(fh);
-     fh = -1;
-    } else if ( mes.cmd != ROAR_CMD_OK ) {
-     if ( fh != -1 )
-      close(fh);
-     fh = -1;
-    }
-   } else {
-    // we don't need to check the content as we know it failed...
-    if ( roar_recv_message(con, &mes, NULL) == -1 ) {
-     close(listen);
-     return -1;
-    }
-
-    if ( mes.cmd != ROAR_CMD_OK ) {
-     close(listen);
-     if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
-      return -1;
-
-     return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
-    } else { // seems like we have a positive reply. So we retry the listen socket:
-     FD_ZERO(&fds);
-     FD_SET(listen, &fds);
-     timeout.tv_sec = 0;
-     timeout.tv_usec = 128000L;
-     fh = -1;
-     if ( select(listen + 1, &fds, NULL, NULL, &timeout) > 0 ) {
-      if ( (fh = accept(listen, NULL, NULL)) == -1 ) {
-       close(listen);
-       if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
-        return -1;
-
-       return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
-      }
+   } else { // seems like we have a positive reply. So we retry the listen socket:
+    FD_ZERO(&fds);
+    FD_SET(listen, &fds);
+    timeout.tv_sec = 0;
+    timeout.tv_usec = 128000L;
+    *fh = -1;
+    if ( select(listen + 1, &fds, NULL, NULL, &timeout) > 0 ) {
+     if ( (*fh = accept(listen, NULL, NULL)) == -1 ) {
+      close(listen);
+      if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 )
+       return -1;
+
+      return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
      }
     }
    }
   }
-
-  close(listen);
+ }
+
+ close(listen);
+
+ return 0;
 #else
   roar_err_set(ROAR_ERROR_NOSYS);
   return -1;
 #endif
+}
+
+static int _roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, int dir, int mixer) {
+ struct roar_libroar_config * config = roar_libroar_get_config();
+ int fh = -1;
+#ifdef ROAR_HAVE_UNIX
+ int socks[2]; // for socketpair()
+#endif
+ struct roar_sockname sockname;
+
+ ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
+
+ if ( config != NULL ) {
+  if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) {
+   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
+  }
+ }
+
+ ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
+
+ roar_libroar_nowarn();
+ if ( roar_vio_ctl(roar_get_connection_vio2(con), ROAR_VIO_CTL_GET_SOCKNAME, &sockname) == -1 ) {
+  roar_libroar_warn();
+#ifdef ROAR_OS_OPENBSD
+  sockname.type = ROAR_SOCKET_TYPE_UNIX;
+#else
+  ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = -1", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
+
+  return -1;
+#endif
+ }
+ roar_libroar_warn();
+
+ ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer);
+
+ if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
+  return -1;
+ }
+
+ if ( roar_stream_connect(con, s, dir, mixer) == -1 ) {
+  return -1;
+ }
+
+ if ( sockname.type != ROAR_SOCKET_TYPE_UNIX ) {
+  if ( _roar_simple_new_stream_obj_try_select(con, s, rate, channels, bits, codec, dir, mixer, &sockname, &fh) == -1 )
+   return -1;
  } else { // this is sockname.type == ROAR_SOCKET_TYPE_UNIX
 #ifdef ROAR_HAVE_UNIX
