diff --git a/gst/tcp/gsttcpserversink.c b/gst/tcp/gsttcpserversink.c
index 723c33a..a9cc455 100644
--- a/gst/tcp/gsttcpserversink.c
+++ b/gst/tcp/gsttcpserversink.c
@@ -267,6 +267,8 @@ gst_tcp_server_sink_init_send (GstMultiFdSink * parent)
 {
   int ret;
   GstTCPServerSink *this = GST_TCP_SERVER_SINK (parent);
+  socklen_t addrlen;
+  struct sockaddr_in server_addr;
 
   /* create sending server socket */
   if ((this->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
@@ -300,6 +302,15 @@ gst_tcp_server_sink_init_send (GstMultiFdSink * parent)
   if (ret)
     goto bind_failed;
 
+  /* get sockname (for example if setting port to 0 this is useful) */
+  addrlen = sizeof (server_addr);
+  ret = getsockname (this->server_sock.fd, (struct sockaddr *) &server_addr,
+      &addrlen);
+  if (ret == 0) {
+    this->server_port = ntohs (server_addr.sin_port);
+    GST_DEBUG_OBJECT (this, "real port used is %d", this->server_port);
+  }
+
   /* set the server socket to nonblocking */
   fcntl (this->server_sock.fd, F_SETFL, O_NONBLOCK);
 
