Index: include/libroar/basic.h
===================================================================
--- include/libroar/basic.h	(revision 1320)
+++ include/libroar/basic.h	(revision 1660)
@@ -55,5 +55,5 @@
 
 struct roar_connection {
- int fh;
+ int __fh;
 };
 
@@ -63,4 +63,5 @@
 int roar_connect    (struct roar_connection * con, char * server);
 int roar_connect_fh (struct roar_connection * con, int fh);
+int roar_get_connection_fh (struct roar_connection * con);
 int roar_disconnect (struct roar_connection * con);
 
Index: libroar/basic.c
===================================================================
--- libroar/basic.c	(revision 1475)
+++ libroar/basic.c	(revision 1660)
@@ -187,8 +187,12 @@
  memset(con, 0, sizeof(struct roar_connection));
 
- con->fh = fh;
+ con->__fh = fh;
 
  roar_errno = ROAR_ERROR_NONE;
  return 0;
+}
+
+int roar_get_connection_fh (struct roar_connection * con) {
+ return con->__fh;
 }
 
@@ -204,8 +208,8 @@
 
 #ifdef ROAR_HAVE_IO_POSIX
- close(con->fh);
-#endif
-
- con->fh = -1;
+ close(roar_get_connection_fh(con));
+#endif
+
+ roar_connect_fh(con, -2);
 
  roar_errno = ROAR_ERROR_NONE;
@@ -256,5 +260,5 @@
  struct roar_vio_calls vio;
 
- if ( roar_vio_open_fh_socket(&vio, con->fh) == -1 )
+ if ( roar_vio_open_fh_socket(&vio, roar_get_connection_fh(con)) == -1 )
   return -1;
 
@@ -298,5 +302,5 @@
  struct roar_vio_calls vio;
 
- if ( roar_vio_open_fh_socket(&vio, con->fh) == -1 )
+ if ( roar_vio_open_fh_socket(&vio, roar_get_connection_fh(con)) == -1 )
   return -1;
 
@@ -385,5 +389,5 @@
  struct roar_vio_calls vio;
 
- if ( roar_vio_open_fh_socket(&vio, con->fh) == -1 )
+ if ( roar_vio_open_fh_socket(&vio, roar_get_connection_fh(con)) == -1 )
   return -1;
 
Index: libroar/file.c
===================================================================
--- libroar/file.c	(revision 1474)
+++ libroar/file.c	(revision 1660)
@@ -203,4 +203,6 @@
  struct roar_stream localstream[1];
 
+ // FIXME: check error cases
+
  if ( !s )
   s = localstream;
@@ -255,7 +257,10 @@
   }
 
-  ROAR_SHUTDOWN(con->fh, SHUT_RD);
-
-  out = con->fh;
+  if ( (out = roar_get_connection_fh(con)) == -1 ) {
+   close(in);
+   return -1;
+  }
+
+  ROAR_SHUTDOWN(out, SHUT_RD);
  } else {
   if ( !(passfh && seek) ) {
Index: libroar/simple.c
===================================================================
--- libroar/simple.c	(revision 1476)
+++ libroar/simple.c	(revision 1660)
@@ -65,4 +65,5 @@
 int roar_simple_stream_obj  (struct roar_stream * s, int rate, int channels, int bits, int codec, char * server, int dir, char * name) {
  struct roar_connection con;
+ int ret;
 
  if ( roar_simple_connect(&con, server, name) == -1 ) {
@@ -87,11 +88,16 @@
  }
 
+ if ( (ret = roar_get_connection_fh(&con)) == -1 ) {
+  roar_disconnect(&con);
+  return -1;
+ }
+
  if ( dir == ROAR_DIR_PLAY ) {
-  ROAR_SHUTDOWN(con.fh, SHUT_RD);
+  ROAR_SHUTDOWN(ret, SHUT_RD);
  } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
-  ROAR_SHUTDOWN(con.fh, SHUT_WR);
- }
-
- return con.fh;
+  ROAR_SHUTDOWN(ret, SHUT_WR);
+ }
+
+ return ret;
 }
 
@@ -146,5 +152,5 @@
 
 #ifdef ROAR_HAVE_BSDSOCKETS
- if ( getsockname(con->fh, (struct sockaddr *)&socket_addr, &len) == -1 ) {
+ if ( getsockname(roar_get_connection_fh(con), (struct sockaddr *)&socket_addr, &len) == -1 ) {
   return -1;
  }
@@ -339,5 +345,6 @@
  struct roar_connection con;
 
- con.fh = fh;
+ if ( roar_connect_fh(&con, fh) == -1 )
+  return -1;
 
  return roar_get_standby(&con);
Index: libroar/stream.c
===================================================================
--- libroar/stream.c	(revision 1475)
+++ libroar/stream.c	(revision 1660)
@@ -203,4 +203,5 @@
 int roar_stream_passfh  (struct roar_connection * con, struct roar_stream * s, int fh) {
  struct roar_message m;
+ int confh;
 
  m.cmd     = ROAR_CMD_PASSFH;
@@ -210,4 +211,7 @@
 
  ROAR_DBG("roar_stream_passfh(con={.fh=%i,...}, s={.id=%i,...}, fh=%i) = ?", con->fh, s->id, fh);
+
+ if ( (confh = roar_get_connection_fh(con)) == -1 )
+  return -1;
 
  if ( roar_send_message(con, &m, NULL) == -1 ) {
@@ -218,5 +222,5 @@
  ROAR_DBG("roar_stream_passfh(*): msg send");
 
- if ( roar_socket_send_fh(con->fh, fh, NULL, 0) == -1 )
+ if ( roar_socket_send_fh(confh, fh, NULL, 0) == -1 )
   return -1;
 
Index: libroaresd/esdbasic.c
===================================================================
--- libroaresd/esdbasic.c	(revision 705)
+++ libroaresd/esdbasic.c	(revision 1660)
@@ -45,4 +45,5 @@
 int esd_open_sound( const char *host ) {
  struct roar_connection con;
+ int fh;
 
  if ( roar_simple_connect(&con, (char*) host, NULL) == -1 ) {
@@ -51,5 +52,10 @@
  }
 
- return con.fh;
+ if ( (fh = roar_get_connection_fh(&con)) == -1 ) {
+  roar_disconnect(&con);
+  return -1;
+ }
+
+ return fh;
 }
 
@@ -79,5 +85,5 @@
  m.datalen = 0;
 
- con.fh = esd;
+ roar_connect_fh(&con, esd);
 
  gettimeofday(&try, NULL);
Index: libroaresd/esdctl.c
===================================================================
--- libroaresd/esdctl.c	(revision 705)
+++ libroaresd/esdctl.c	(revision 1660)
@@ -54,5 +54,5 @@
  struct roar_connection con;
 
- con.fh = esd;
+ roar_connect_fh(&con, esd);
 
  return roar_set_standby(&con, ROAR_STANDBY_ACTIVE);
@@ -62,5 +62,5 @@
  struct roar_connection con;
 
- con.fh = esd;
+ roar_connect_fh(&con, esd);
 
  return roar_set_standby(&con, ROAR_STANDBY_INACTIVE);
@@ -148,5 +148,5 @@
  r->version = 0; // seems to be static
 
- con.fh    = esd;
+ roar_connect_fh(&con, esd);
  m.cmd     = ROAR_CMD_SERVER_OINFO;
  m.datalen = 0;
@@ -201,5 +201,5 @@
  esd_player_info_t * new_player, * cur = NULL; // = NULL to avoid gcc warning
 
- con->fh = esd;
+ roar_connect_fh(con, esd);
 
  r = malloc(sizeof(esd_info_t));
@@ -334,5 +334,5 @@
  struct roar_mixer_settings mixer;
 
- con.fh = esd;
+ roar_connect_fh(&con, esd);
 
  mixer.mixer[0] = left_scale  == 256 ? 65535 : left_scale  * 256;
Index: libroaryiff/audiocd.c
===================================================================
--- libroaryiff/audiocd.c	(revision 819)
+++ libroaryiff/audiocd.c	(revision 1660)
@@ -42,5 +42,5 @@
   return -1;
 
- rcon.fh = con->fd;
+ roar_connect_fh(&rcon, con->fd);
 
  if ( ! _g_roaryiff_cdrom.is_open ) {
@@ -58,5 +58,5 @@
   return -1;
 
- rcon.fh = con->fd;
+ roar_connect_fh(&rcon, con->fd);
 
  if ( _g_roaryiff_cdrom.is_playing ) {
Index: libroaryiff/connection.c
===================================================================
--- libroaryiff/connection.c	(revision 787)
+++ libroaryiff/connection.c	(revision 1660)
@@ -52,5 +52,9 @@
  }
 
- ycon->fd = con.fh;
+ if ( (ycon->fd = roar_get_connection_fh(&con)) == -1 ) {
+  roar_disconnect(&con);
+  free(ycon);
+  return NULL;
+ }
 
  return ycon;
Index: libroaryiff/ctl.c
===================================================================
--- libroaryiff/ctl.c	(revision 788)
+++ libroaryiff/ctl.c	(revision 1660)
@@ -31,5 +31,5 @@
   return;
 
- rcon.fh = con->fd;
+ roar_connect_fh(&rcon, con->fd);
 
  roar_exit(&rcon);
Index: libroaryiff/events.c
===================================================================
--- libroaryiff/events.c	(revision 799)
+++ libroaryiff/events.c	(revision 1660)
@@ -34,5 +34,5 @@
   return -1;
 
- rcon.fh = con->fd;
+ roar_connect_fh(&rcon, con->fd);
 
  if ( con->prev_generated_yid != YIDNULL ) {
Index: libroaryiff/playback.c
===================================================================
--- libroaryiff/playback.c	(revision 794)
+++ libroaryiff/playback.c	(revision 1660)
@@ -39,5 +39,5 @@
   return YIDNULL;
 
- rcon.fh = con->fd;
+ roar_connect_fh(&rcon, con->fd);
 
  // hm,... find out how to do this.
@@ -62,5 +62,5 @@
   return;
 
- rcon.fh = con->fd;
+ roar_connect_fh(&rcon, con->fd);
 
  roar_kick(&rcon, ROAR_OT_STREAM, ROARYIFF_YID2ROAR(yid));
