diff --git a/src/core/tcp.c b/src/core/tcp.c
index bd7d64ec..402fbd19 100644
--- a/src/core/tcp.c
+++ b/src/core/tcp.c
@@ -1015,9 +1015,14 @@ tcp_new_port(void)
   struct tcp_pcb *pcb;
 
 again:
+#if TCP_RANDOM_PORT && defined(LWIP_RAND)
+  tcp_port = (u16_t)(LWIP_RAND() % (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START))
+             + TCP_LOCAL_PORT_RANGE_START;
+#else
   tcp_port++;
   if (tcp_port == TCP_LOCAL_PORT_RANGE_END) {
     tcp_port = TCP_LOCAL_PORT_RANGE_START;
   }
+#endif
   /* Check all PCB lists. */
   for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
