diff --git a/gst/tcp/gsttcpserversink.c b/gst/tcp/gsttcpserversink.c
index fdca0ec..9233002 100644
--- a/gst/tcp/gsttcpserversink.c
+++ b/gst/tcp/gsttcpserversink.c
@@ -305,6 +305,23 @@ gst_tcp_server_sink_init_send (GstMultiFdSink * parent)
   if (ret)
     goto bind_failed;
 
+  /* get real port number if bound to port 0 (auto) */
+  if (this->server_port == 0) {
+    socklen_t addrlen;
+    struct sockaddr_in server_addr;
+
+    addrlen = sizeof (server_addr);
+    ret = getsockname (this->server_sock.fd, (struct sockaddr *) &server_addr,
+      &addrlen);
+    if (ret == 0) {
+      this->server_sin = server_addr;
+      this->server_port = ntohs (server_addr.sin_port);
+      GST_DEBUG_OBJECT (this, "automatic port binding: real port used is %d", 
+        this->server_port);
+      g_object_notify(this, "port");
+    }
+  }
+
   /* set the server socket to nonblocking */
   fcntl (this->server_sock.fd, F_SETFL, O_NONBLOCK);
 
diff --git a/gst/tcp/gsttcpserversrc.c b/gst/tcp/gsttcpserversrc.c
index 35d2863..2748ed4 100644
--- a/gst/tcp/gsttcpserversrc.c
+++ b/gst/tcp/gsttcpserversrc.c
@@ -391,6 +391,23 @@ gst_tcp_server_src_start (GstBaseSrc * bsrc)
               sizeof (src->server_sin))) < 0)
     goto bind_error;
 
+  /* get real port number if bound to port 0 (auto) */
+  if (src->server_port == 0) {
+    socklen_t addrlen;
+    struct sockaddr_in server_addr;
+
+    addrlen = sizeof (server_addr);
+    ret = getsockname (src->server_sock_fd.fd, (struct sockaddr *) &server_addr,
+      &addrlen);
+    if (ret == 0) {
+      src->server_sin = server_addr;
+      src->server_port = ntohs (server_addr.sin_port);
+      GST_DEBUG_OBJECT (src, "automatic port binding: real port used is %d",
+        src->server_port);
+      g_object_notify(src, "port");
+    }
+  }
+
   GST_DEBUG_OBJECT (src, "listening on server socket %d with queue of %d",
       src->server_sock_fd.fd, TCP_BACKLOG);
 
