Index: libroar/vio_stream.c
===================================================================
--- libroar/vio_stream.c	(revision 6027)
+++ libroar/vio_stream.c	(revision 6028)
@@ -155,5 +155,5 @@
 }
 
-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) {
+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, int * can_go_on) {
 #ifdef ROAR_HAVE_SELECT
  int confh;
@@ -184,30 +184,36 @@
  memset(&socket_addr, 0, sizeof(socket_addr));
 
- if ( sockname->type == ROAR_SOCKET_TYPE_DECNET ) {
+ *can_go_on = 0;
+
+ switch (sockname->type) {
 #ifdef ROAR_HAVE_LIBDNET
-  if ( roar_socket_get_local_nodename() != NULL && (binaddr = getnodeadd()) != NULL ) {
-   snprintf(socketaddr+3, sizeof(socketaddr)-3, "roar$TMP%04x%02x", getpid(), count++);
-   snprintf(file, sizeof(file), "%s::%s", roar_socket_get_local_nodename(), socketaddr+3);
-   memcpy(socketaddr, binaddr->a_addr, 2);
-   socketaddr[2] = 0; // object 0.
-   socketaddr_len = 3 + roar_mm_strlen(socketaddr+3);
-  } else {
-   return -1;
-  }
-#else
-  roar_err_set(ROAR_ERROR_NOSYS);
-  return -1;
+  case ROAR_SOCKET_TYPE_DECNET:
+    if ( roar_socket_get_local_nodename() != NULL && (binaddr = getnodeadd()) != NULL ) {
+     snprintf(socketaddr+3, sizeof(socketaddr)-3, "roar$TMP%04x%02x", getpid(), count++);
+     snprintf(file, sizeof(file), "%s::%s", roar_socket_get_local_nodename(), socketaddr+3);
+     memcpy(socketaddr, binaddr->a_addr, 2);
+     socketaddr[2] = 0; // object 0.
+     socketaddr_len = 3 + roar_mm_strlen(socketaddr+3);
+    } else {
+     return -1;
+    }
+   break;
 #endif
 #ifdef ROAR_HAVE_IPV4
- } else {
-  strncpy(file, sockname->addr, sizeof(file) - 1);
-  roar_err_set(ROAR_ERROR_NONE);
-  if ( inet_aton(sockname->addr, &socket_addr.sin_addr) == 0 ) {
-   roar_err_update();
-   return -1;
-  }
-  memcpy(socketaddr, &socket_addr.sin_addr.s_addr, 4);
-  socketaddr_len = 6;
-#endif
+  case ROAR_SOCKET_TYPE_TCP:
+    strncpy(file, sockname->addr, sizeof(file) - 1);
+    roar_err_set(ROAR_ERROR_NONE);
+    if ( inet_aton(sockname->addr, &socket_addr.sin_addr) == 0 ) {
+     roar_err_update();
+     return -1;
+    }
+    memcpy(socketaddr, &socket_addr.sin_addr.s_addr, 4);
+    socketaddr_len = 6;
+   break;
+#endif
+  default:
+    roar_err_set(ROAR_ERROR_AFNOTSUP);
+    return -1;
+   break;
  }
 
@@ -219,27 +225,30 @@
  roar_libroar_warn();
 
- if ( sockname->type == ROAR_SOCKET_TYPE_INET ) {
+ switch (sockname->type) {
 #ifdef ROAR_HAVE_IPV4
-  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int));
-
-  len = sizeof(struct sockaddr_in);
-  if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) {
-   return -1;
-  }
-  ((uint16_t*)socketaddr)[3] = socket_addr.sin_port;
-  port = ROAR_NET2HOST16(socket_addr.sin_port);
-  ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port);
-#else
-  return -1;
-#endif
- } else if ( sockname->type == ROAR_SOCKET_TYPE_DECNET ) {
+  case ROAR_SOCKET_TYPE_TCP:
+    setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int));
+
+    len = sizeof(struct sockaddr_in);
+    if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) {
+     return -1;
+    }
+    ((uint16_t*)socketaddr)[3] = socket_addr.sin_port;
+    port = ROAR_NET2HOST16(socket_addr.sin_port);
+    ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port);
+   break;
+#endif
 #ifdef ROAR_HAVE_LIBDNET
-  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
-#else
-  return -1;
-#endif
- }
-
- if ( roar_stream_connect_to_advanced(con, s, 0, 1, 0, sockname->type, socketaddr_len, socketaddr, ROAR_PROTO_NONE, -1, NULL, 0, NULL) != -1 ) {
+  case ROAR_SOCKET_TYPE_DECNET:
+    setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
+   break;
+#endif
+ }
+
+ if ( roar_stream_connect_to_advanced(con, s, 0, 1, 0, sockname->type, socketaddr_len, socketaddr, ROAR_PROTO_NONE, -1, NULL, 0, NULL) == -1 ) {
+  close(listen);
+  *can_go_on = 1;
+  return -1;
+ } else {
 
   FD_ZERO(&fds);
@@ -262,5 +271,6 @@
     return -1;
 
-   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
+   *can_go_on = 1;
+   return -1;
   }
 
@@ -291,5 +301,6 @@
      return -1;
 
-    return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
+    *can_go_on = 1;
+    return -1;
    } else { // seems like we have a positive reply. So we retry the listen socket:
     FD_ZERO(&fds);
@@ -304,17 +315,17 @@
        return -1;
 
-      return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
+      *can_go_on = 1;
+      return -1;
      }
     }
    }
   }
- }
-
- close(listen);
-
- return 0;
+  close(listen);
+  return 0;
+ }
+
 #else
-  roar_err_set(ROAR_ERROR_NOSYS);
-  return -1;
+ roar_err_set(ROAR_ERROR_NOSYS);
+ return -1;
 #endif
 }
@@ -327,4 +338,5 @@
 #endif
  struct roar_sockname sockname;
+ int can_go_on = 0;
 
  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);
@@ -362,6 +374,14 @@
 
  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;
+  do {
+   if ( _roar_simple_new_stream_obj_try_select(con, s,
+                                               rate, channels, bits, codec, dir, mixer,
+                                               &sockname, &fh, &can_go_on) != -1 )
+    break;
+   if ( !can_go_on )
+    return -1;
+
+   return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer);
+  } while (0);
  } else { // this is sockname.type == ROAR_SOCKET_TYPE_UNIX
 #ifdef ROAR_HAVE_UNIX
