fixes #658 Add parallelized travis builds and tests
diff --git a/.travis.yml b/.travis.yml
index 7055e80..a2131ba 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -21,8 +21,8 @@
   - cd build
   # Perform CMake backend generation, build, and test
   - cmake ..
-  - cmake --build .
-  - ctest --output-on-failure -C Debug
+  - cmake --build . -- -j4
+  - ctest --output-on-failure -C Debug -j4
 deploy:
   provider: releases
   api_key:
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4ca4d61..4518fb1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,6 +3,7 @@
 #   Copyright (c) 2013 GoPivotal, Inc.  All rights reserved.
 #   Copyright (c) 2015-2016 Jack R. Dunaway. All rights reserved.
 #   Copyright 2016 Garrett D'Amore <garrett@damore.org>
+#   Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 #
 #   Permission is hereby granted, free of charge, to any person obtaining a copy
 #   of this software and associated documentation files (the "Software"),
@@ -211,12 +212,14 @@
     enable_testing ()
     set (all_tests "")
 
+    set (TEST_PORT 5500)
     macro (add_libnanomsg_test NAME TIMEOUT)
         list (APPEND all_tests ${NAME})
         add_executable (${NAME} tests/${NAME}.c)
         target_link_libraries (${NAME} ${PROJECT_NAME})
-        add_test (NAME ${NAME} COMMAND ${NAME})
+        add_test (NAME ${NAME} COMMAND ${NAME} ${TEST_PORT})
         set_tests_properties (${NAME} PROPERTIES TIMEOUT ${TIMEOUT})
+        math (EXPR TEST_PORT "${TEST_PORT}+10")
     endmacro (add_libnanomsg_test)
 
     #  Transport tests.
diff --git a/tests/async_shutdown.c b/tests/async_shutdown.c
index 96cda89..4c44d89 100644
--- a/tests/async_shutdown.c
+++ b/tests/async_shutdown.c
@@ -1,6 +1,7 @@
 /*
     Copyright (c) 2012 Martin Sustrik  All rights reserved.
     Copyright (c) 2015 Jack R. Dunaway.  All rights reserved.
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -35,7 +36,6 @@
 /*  Test condition of closing sockets that are blocking in another thread. */
 
 #define TEST_LOOPS 10
-#define SOCKET_ADDRESS "tcp://127.0.0.1:5557"
 
 struct nn_atomic active;
 
@@ -56,15 +56,19 @@
     errno_assert (nn_errno () == EBADF);
 }
 
-int main ()
+int main (int argc, const char *argv[])
 {
     int sb;
     int i;
     struct nn_thread thread;
+    char socket_address[128];
+
+    test_addr_from(socket_address, "tcp", "127.0.0.1",
+            get_test_port(argc, argv));
 
     for (i = 0; i != TEST_LOOPS; ++i) {
         sb = test_socket (AF_SP, NN_PULL);
-        test_bind (sb, SOCKET_ADDRESS);
+        test_bind (sb, socket_address);
         nn_sleep (100);
         nn_thread_init (&thread, routine, &sb);
         nn_sleep (100);
diff --git a/tests/bug328.c b/tests/bug328.c
index 3e203f6..76d0725 100644
--- a/tests/bug328.c
+++ b/tests/bug328.c
@@ -1,5 +1,6 @@
 /*
     Copyright (c) 2012 Martin Sustrik  All rights reserved.
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -25,20 +26,22 @@
 
 #include "testutil.h"
 
-#define SOCKET_ADDRESS "tcp://127.0.0.1:5777"
-
-int main ()
+int main (int argc, const char *argv[])
 {
     int sb;
     int sc;
     int s1;
+    char socket_address[128];
+
+    test_addr_from(socket_address, "tcp", "127.0.0.1",
+            get_test_port(argc, argv));
 
     sb = test_socket (AF_SP, NN_PAIR);
-    test_bind (sb, SOCKET_ADDRESS);
+    test_bind (sb, socket_address);
     s1 = test_socket (AF_SP, NN_PAIR);
-    test_bind (s1, SOCKET_ADDRESS);
+    test_bind (s1, socket_address);
     sc = test_socket (AF_SP, NN_PAIR);
-    test_connect (sc, SOCKET_ADDRESS);
+    test_connect (sc, socket_address);
 
     nn_sleep(100);
     test_send (sc, "ABC");
diff --git a/tests/cmsg.c b/tests/cmsg.c
index 105cd04..2c2c67e 100644
--- a/tests/cmsg.c
+++ b/tests/cmsg.c
@@ -1,6 +1,7 @@
 /*
     Copyright (c) 2014 Martin Sustrik  All rights reserved.
     Copyright 2015 Garrett D'Amore <garrett@damore.org>
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -27,9 +28,7 @@
 
 #include "testutil.h"
 
-#define SOCKET_ADDRESS "tcp://127.0.0.1:5555"
-
-int main ()
+int main (int argc, const char *argv[])
 {
     int rc;
     int rep;
@@ -41,11 +40,15 @@
     struct nn_cmsghdr *cmsg;
     unsigned char *data;
     void *buf;
+    char socket_address[128];
+
+    test_addr_from(socket_address, "tcp", "127.0.0.1",
+            get_test_port(argc, argv));
     
     rep = test_socket (AF_SP_RAW, NN_REP);
-    test_bind (rep, SOCKET_ADDRESS);
+    test_bind (rep, socket_address);
     req = test_socket (AF_SP, NN_REQ);
-    test_connect (req, SOCKET_ADDRESS);
+    test_connect (req, socket_address);
 
     /* Test ancillary data in static buffer. */
 
diff --git a/tests/device4.c b/tests/device4.c
index 6ff0275..9e03516 100644
--- a/tests/device4.c
+++ b/tests/device4.c
@@ -2,6 +2,7 @@
     Copyright (c) 2012 Martin Sustrik  All rights reserved.
     Copyright (c) 2013 GoPivotal, Inc.  All rights reserved.
     Copyright 2015 Garrett D'Amore <garrett@damore.org>
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -30,8 +31,7 @@
 #include "../src/utils/attr.h"
 #include "../src/utils/thread.c"
 
-#define SOCKET_ADDRESS_F "tcp://127.0.0.1:5565"
-#define SOCKET_ADDRESS_G "tcp://127.0.0.1:5566"
+static char socket_address_f[128], socket_address_g[128];
 
 void device4 (NN_UNUSED void *arg)
 {
@@ -41,9 +41,9 @@
 
     /*  Intialise the device sockets. */
     devf = test_socket (AF_SP_RAW, NN_REP);
-    test_bind (devf, SOCKET_ADDRESS_F);
+    test_bind (devf, socket_address_f);
     devg = test_socket (AF_SP_RAW, NN_REQ);
-    test_bind (devg, SOCKET_ADDRESS_G);
+    test_bind (devg, socket_address_g);
 
     /*  Run the device. */
     rc = nn_device (devf, devg);
@@ -54,12 +54,17 @@
     test_close (devf);
 }
 
-int main ()
+int main (int argc, const char *argv[])
 {
     int endf;
     int endg;
     struct nn_thread thread4;
 
+    int port = get_test_port(argc, argv);
+
+    test_addr_from(socket_address_f, "tcp", "127.0.0.1", port);
+    test_addr_from(socket_address_g, "tcp", "127.0.0.1", port + 1);
+
     /*  Test the bi-directional device with REQ/REP (headers). */
 
     /*  Start the device. */
@@ -67,9 +72,9 @@
 
     /*  Create two sockets to connect to the device. */
     endf = test_socket (AF_SP, NN_REQ);
-    test_connect (endf, SOCKET_ADDRESS_F);
+    test_connect (endf, socket_address_f);
     endg = test_socket (AF_SP, NN_REP);
-    test_connect (endg, SOCKET_ADDRESS_G);
+    test_connect (endg, socket_address_g);
 
     /*  Wait for TCP to establish. */
     nn_sleep (100);
diff --git a/tests/device5.c b/tests/device5.c
index 6030c3b..0df5676 100644
--- a/tests/device5.c
+++ b/tests/device5.c
@@ -2,6 +2,7 @@
     Copyright (c) 2012 Martin Sustrik  All rights reserved.
     Copyright (c) 2013 GoPivotal, Inc.  All rights reserved.
     Copyright 2015 Garrett D'Amore <garrett@damore.org>
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -30,9 +31,7 @@
 #include "../src/utils/attr.h"
 #include "../src/utils/thread.c"
 
-#define SOCKET_ADDRESS_H "tcp://127.0.0.1:5567"
-#define SOCKET_ADDRESS_I "tcp://127.0.0.1:5568"
-#define SOCKET_ADDRESS_J "tcp://127.0.0.1:5569"
+static char socket_address_h[128], socket_address_i[128], socket_address_j[128];
 
 void device5 (NN_UNUSED void *arg)
 {
@@ -42,9 +41,9 @@
 
     /*  Intialise the device sockets. */
     dev0 = test_socket (AF_SP_RAW, NN_REP);
-    test_bind (dev0, SOCKET_ADDRESS_H);
+    test_bind (dev0, socket_address_h);
     dev1 = test_socket (AF_SP_RAW, NN_REQ);
-    test_bind (dev1, SOCKET_ADDRESS_I);
+    test_bind (dev1, socket_address_i);
 
     /*  Run the device. */
     rc = nn_device (dev0, dev1);
@@ -62,9 +61,9 @@
     int dev3;
 
     dev2 = test_socket (AF_SP_RAW, NN_REP);
-    test_connect (dev2, SOCKET_ADDRESS_I);
+    test_connect (dev2, socket_address_i);
     dev3 = test_socket (AF_SP_RAW, NN_REQ);
-    test_bind (dev3, SOCKET_ADDRESS_J);
+    test_bind (dev3, socket_address_j);
 
     /*  Run the device. */
     rc = nn_device (dev2, dev3);
@@ -75,13 +74,19 @@
     test_close (dev3);
 }
 
-int main ()
+int main (int argc, const char *argv[])
 {
     int end0;
     int end1;
     struct nn_thread thread5;
     struct nn_thread thread6;
 
+    int port = get_test_port(argc, argv);
+
+    test_addr_from(socket_address_h, "tcp", "127.0.0.1", port);
+    test_addr_from(socket_address_i, "tcp", "127.0.0.1", port + 1);
+    test_addr_from(socket_address_j, "tcp", "127.0.0.1", port + 2);
+
     /*  Test the bi-directional device with REQ/REP (headers). */
 
     /*  Start the devices. */
@@ -90,9 +95,9 @@
 
     /*  Create two sockets to connect to the device. */
     end0 = test_socket (AF_SP, NN_REQ);
-    test_connect (end0, SOCKET_ADDRESS_H);
+    test_connect (end0, socket_address_h);
     end1 = test_socket (AF_SP, NN_REP);
-    test_connect (end1, SOCKET_ADDRESS_J);
+    test_connect (end1, socket_address_j);
 
     /*  Wait for TCP to establish. */
     nn_sleep (100);
diff --git a/tests/device6.c b/tests/device6.c
index a72ce9b..313282e 100644
--- a/tests/device6.c
+++ b/tests/device6.c
@@ -2,6 +2,7 @@
     Copyright (c) 2012 Martin Sustrik  All rights reserved.
     Copyright (c) 2013 GoPivotal, Inc.  All rights reserved.
     Copyright 2015 Garrett D'Amore <garrett@damore.org>
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -30,9 +31,7 @@
 #include "../src/utils/attr.h"
 #include "../src/utils/thread.c"
 
-#define SOCKET_ADDRESS_H "tcp://127.0.0.1:5570"
-#define SOCKET_ADDRESS_I "tcp://127.0.0.1:5571"
-#define SOCKET_ADDRESS_J "tcp://127.0.0.1:5572"
+static char socket_address_h[128], socket_address_i[128], socket_address_j[128];
 
 void device5 (NN_UNUSED void *arg)
 {
@@ -42,9 +41,9 @@
 
     /*  Intialise the device sockets. */
     dev0 = test_socket (AF_SP_RAW, NN_RESPONDENT);
-    test_bind (dev0, SOCKET_ADDRESS_H);
+    test_bind (dev0, socket_address_h);
     dev1 = test_socket (AF_SP_RAW, NN_SURVEYOR);
-    test_bind (dev1, SOCKET_ADDRESS_I);
+    test_bind (dev1, socket_address_i);
 
     /*  Run the device. */
     rc = nn_device (dev0, dev1);
@@ -62,9 +61,9 @@
     int dev3;
 
     dev2 = test_socket (AF_SP_RAW, NN_RESPONDENT);
-    test_connect (dev2, SOCKET_ADDRESS_I);
+    test_connect (dev2, socket_address_i);
     dev3 = test_socket (AF_SP_RAW, NN_SURVEYOR);
-    test_bind (dev3, SOCKET_ADDRESS_J);
+    test_bind (dev3, socket_address_j);
 
     /*  Run the device. */
     rc = nn_device (dev2, dev3);
@@ -75,13 +74,19 @@
     test_close (dev3);
 }
 
-int main ()
+int main (int argc, const char *argv[])
 {
     int end0;
     int end1;
     struct nn_thread thread5;
     struct nn_thread thread6;
 
+    int port = get_test_port(argc, argv);
+
+    test_addr_from(socket_address_h, "tcp", "127.0.0.1", port);
+    test_addr_from(socket_address_i, "tcp", "127.0.0.1", port + 1);
+    test_addr_from(socket_address_j, "tcp", "127.0.0.1", port + 2);
+
     /*  Test the bi-directional device with SURVEYOR(headers). */
 
     /*  Start the devices. */
@@ -90,9 +95,9 @@
 
     /*  Create two sockets to connect to the device. */
     end0 = test_socket (AF_SP, NN_SURVEYOR);
-    test_connect (end0, SOCKET_ADDRESS_H);
+    test_connect (end0, socket_address_h);
     end1 = test_socket (AF_SP, NN_RESPONDENT);
-    test_connect (end1, SOCKET_ADDRESS_J);
+    test_connect (end1, socket_address_j);
 
     /*  Wait up to a second for TCP to establish. */
     nn_sleep (1000);
diff --git a/tests/device7.c b/tests/device7.c
index ce2999d..d79172f 100644
--- a/tests/device7.c
+++ b/tests/device7.c
@@ -2,6 +2,7 @@
     Copyright (c) 2012 Martin Sustrik  All rights reserved.
     Copyright (c) 2013 GoPivotal, Inc.  All rights reserved.
     Copyright 2015 Garrett D'Amore <garrett@damore.org>
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -31,9 +32,9 @@
 #include "../src/utils/attr.h"
 #include "../src/utils/thread.c"
 
-#define SOCKET_ADDRESS_H "tcp://127.0.0.1:5573"
 #define SOCKET_ADDRESS_I "inproc://nobody"
-#define SOCKET_ADDRESS_J "tcp://127.0.0.1:5575"
+
+static char socket_address_h[128], socket_address_j[128];
 
 void device5 (NN_UNUSED void *arg)
 {
@@ -43,7 +44,7 @@
 
     /*  Intialise the device sockets. */
     dev0 = test_socket (AF_SP_RAW, NN_REP);
-    test_bind (dev0, SOCKET_ADDRESS_H);
+    test_bind (dev0, socket_address_h);
     dev1 = test_socket (AF_SP_RAW, NN_REQ);
     test_bind (dev1, SOCKET_ADDRESS_I);
 
@@ -65,7 +66,7 @@
     dev2 = test_socket (AF_SP_RAW, NN_REP);
     test_connect (dev2, SOCKET_ADDRESS_I);
     dev3 = test_socket (AF_SP_RAW, NN_REQ);
-    test_bind (dev3, SOCKET_ADDRESS_J);
+    test_bind (dev3, socket_address_j);
 
     /*  Run the device. */
     rc = nn_device (dev2, dev3);
@@ -76,13 +77,18 @@
     test_close (dev3);
 }
 
-int main ()
+int main (int argc, const char *argv[])
 {
     int end0;
     int end1;
     struct nn_thread thread5;
     struct nn_thread thread6;
 
+    int port = get_test_port(argc, argv);
+
+    test_addr_from(socket_address_h, "tcp", "127.0.0.1", port);
+    test_addr_from(socket_address_j, "tcp", "127.0.0.1", port + 1);
+
     /*  Test the bi-directional device with REQ/REP (headers). */
 
     /*  Start the devices. */
@@ -91,9 +97,9 @@
 
     /*  Create two sockets to connect to the device. */
     end0 = test_socket (AF_SP, NN_REQ);
-    test_connect (end0, SOCKET_ADDRESS_H);
+    test_connect (end0, socket_address_h);
     end1 = test_socket (AF_SP, NN_REP);
-    test_connect (end1, SOCKET_ADDRESS_J);
+    test_connect (end1, socket_address_j);
 
     /*  Wait for TCP to establish. */
     nn_sleep (1000);
diff --git a/tests/emfile.c b/tests/emfile.c
index 01b0994..79b9ee7 100644
--- a/tests/emfile.c
+++ b/tests/emfile.c
@@ -25,7 +25,6 @@
 #include "../src/tcp.h"
 #include "../src/utils/err.c"
 
-#define SOCKET_ADDRESS "tcp://127.0.0.1:5555"
 #define MAX_SOCKETS 1000
 
 int main ()
diff --git a/tests/msg.c b/tests/msg.c
index d0b006e..d0d9530 100644
--- a/tests/msg.c
+++ b/tests/msg.c
@@ -1,5 +1,6 @@
 /*
     Copyright (c) 2013 Martin Sustrik  All rights reserved.
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -28,11 +29,10 @@
 #include <string.h>
 
 #define SOCKET_ADDRESS "inproc://a"
-#define SOCKET_ADDRESS_TCP "tcp://127.0.0.1:5557"
 
 char longdata[1 << 20];
 
-int main ()
+int main (int argc, const char *argv[])
 {
     int rc;
     int sb;
@@ -41,6 +41,10 @@
     int i;
     struct nn_iovec iov;
     struct nn_msghdr hdr;
+    char socket_address_tcp[128];
+
+    test_addr_from(socket_address_tcp, "tcp", "127.0.0.1",
+            get_test_port(argc, argv));
 
     sb = test_socket (AF_SP, NN_PAIR);
     test_bind (sb, SOCKET_ADDRESS);
@@ -99,9 +103,9 @@
     /*  Test receiving of large message  */
 
     sb = test_socket (AF_SP, NN_PAIR);
-    test_bind (sb, SOCKET_ADDRESS_TCP);
+    test_bind (sb, socket_address_tcp);
     sc = test_socket (AF_SP, NN_PAIR);
-    test_connect (sc, SOCKET_ADDRESS_TCP);
+    test_connect (sc, socket_address_tcp);
 
     for (i = 0; i < (int) sizeof (longdata); ++i)
         longdata[i] = '0' + (i % 10);
diff --git a/tests/separation.c b/tests/separation.c
index f59a3b7..7545d4e 100644
--- a/tests/separation.c
+++ b/tests/separation.c
@@ -1,6 +1,7 @@
 /*
     Copyright (c) 2013 Martin Sustrik  All rights reserved.
     Copyright (c) 2013 GoPivotal, Inc.  All rights reserved.
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -31,17 +32,20 @@
 
 #define SOCKET_ADDRESS_INPROC "inproc://a"
 #define SOCKET_ADDRESS_IPC "ipc://test-separation.ipc"
-#define SOCKET_ADDRESS_TCP "tcp://127.0.0.1:5556"
 
 /*  This test checks whether the library prevents interconnecting sockets
     between different non-compatible protocols. */
 
-int main ()
+int main (int argc, const char *argv[])
 {
     int rc;
     int pair;
     int pull;
     int timeo;
+    char socket_address_tcp[128];
+
+    test_addr_from(socket_address_tcp, "tcp", "127.0.0.1",
+            get_test_port(argc, argv));
 
     /*  Inproc: Bind first, connect second. */
     pair = test_socket (AF_SP, NN_PAIR);
@@ -88,9 +92,9 @@
 
     /*  TCP */
     pair = test_socket (AF_SP, NN_PAIR);
-    test_bind (pair, SOCKET_ADDRESS_TCP);
+    test_bind (pair, socket_address_tcp);
     pull = test_socket (AF_SP, NN_PULL);
-    test_connect (pull, SOCKET_ADDRESS_TCP);
+    test_connect (pull, socket_address_tcp);
     timeo = 100;
     test_setsockopt (pair, NN_SOL_SOCKET, NN_SNDTIMEO,
         &timeo, sizeof (timeo));
diff --git a/tests/shutdown.c b/tests/shutdown.c
index 58ef44c..2dd4e4d 100644
--- a/tests/shutdown.c
+++ b/tests/shutdown.c
@@ -1,5 +1,6 @@
 /*
     Copyright (c) 2013 GoPivotal, Inc.  All rights reserved.
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -26,15 +27,19 @@
 
 #include "testutil.h"
 
-int main ()
+int main (int argc, const char *argv[])
 {
     int s;
     int rc;
     int eid;
+    char socket_address[128];
+
+    test_addr_from(socket_address, "tcp", "127.0.0.1",
+            get_test_port(argc, argv));
 
     /*  Run endpoint shutdown and socket shutdown in parallel. */
     s = test_socket (AF_SP, NN_REQ);
-    eid = test_connect (s, "tcp://127.0.0.1:5590");
+    eid = test_connect (s, socket_address);
     rc = nn_shutdown (s, eid);
     errno_assert (rc == 0);
     test_close (s);
diff --git a/tests/stats.c b/tests/stats.c
index cf7521a..231eb2d 100644
--- a/tests/stats.c
+++ b/tests/stats.c
@@ -1,5 +1,6 @@
 /*
     Copyright 2016 Garrett D'Amore <garrett@damore.org>
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -25,18 +26,20 @@
 
 #include "testutil.h"
 
-#define SOCKET_ADDRESS "tcp://127.0.0.1:5555"
-
-int main (int argc, char **argv)
+int main (int argc, const char *argv[])
 {
     int rep1;
     int req1;
+    char socket_address[128];
+
+    test_addr_from(socket_address, "tcp", "127.0.0.1",
+            get_test_port(argc, argv));
 
     /*  Test req/rep with full socket types. */
     rep1 = test_socket (AF_SP, NN_REP);
-    test_bind (rep1, SOCKET_ADDRESS);
+    test_bind (rep1, socket_address);
     req1 = test_socket (AF_SP, NN_REQ);
-    test_connect (req1, SOCKET_ADDRESS);
+    test_connect (req1, socket_address);
     nn_sleep (200);
 
     nn_assert (nn_get_statistic(rep1, NN_STAT_ACCEPTED_CONNECTIONS) == 1);
diff --git a/tests/tcp.c b/tests/tcp.c
index 2e8aac6..b4c8130 100644
--- a/tests/tcp.c
+++ b/tests/tcp.c
@@ -1,6 +1,7 @@
 /*
     Copyright (c) 2012 Martin Sustrik  All rights reserved.
     Copyright 2015 Garrett D'Amore <garrett@damore.org>
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -30,11 +31,9 @@
 
 /*  Tests TCP transport. */
 
-#define SOCKET_ADDRESS "tcp://127.0.0.1:5555"
-
 int sc;
 
-int main ()
+int main (int argc, const char *argv[])
 {
     int rc;
     int sb;
@@ -43,16 +42,23 @@
     size_t sz;
     int s1, s2;
     void * dummy_buf;
+    char addr[128];
+    char socket_address[128];
+
+    int port = get_test_port(argc, argv);
+
+    test_addr_from(socket_address, "tcp", "127.0.0.1", port);
 
     /*  Try closing bound but unconnected socket. */
     sb = test_socket (AF_SP, NN_PAIR);
-    test_bind (sb, SOCKET_ADDRESS);
+    test_bind (sb, socket_address);
     test_close (sb);
 
     /*  Try closing a TCP socket while it not connected. At the same time
         test specifying the local address for the connection. */
     sc = test_socket (AF_SP, NN_PAIR);
-    test_connect (sc, "tcp://127.0.0.1;127.0.0.1:5555");
+    test_addr_from(addr, "tcp", "127.0.0.1;127.0.0.1", port);
+    test_connect (sc, addr);
     test_close (sc);
 
     /*  Open the socket anew. */
@@ -123,13 +129,13 @@
     errno_assert (nn_errno () == EINVAL);
 
     /*  Connect correctly. Do so before binding the peer socket. */
-    test_connect (sc, SOCKET_ADDRESS);
+    test_connect (sc, socket_address);
 
     /*  Leave enough time for at least on re-connect attempt. */
     nn_sleep (200);
 
     sb = test_socket (AF_SP, NN_PAIR);
-    test_bind (sb, SOCKET_ADDRESS);
+    test_bind (sb, socket_address);
 
     /*  Ping-pong test. */
     for (i = 0; i != 100; ++i) {
@@ -154,11 +160,11 @@
 
     /*  Test whether connection rejection is handled decently. */
     sb = test_socket (AF_SP, NN_PAIR);
-    test_bind (sb, SOCKET_ADDRESS);
+    test_bind (sb, socket_address);
     s1 = test_socket (AF_SP, NN_PAIR);
-    test_connect (s1, SOCKET_ADDRESS);
+    test_connect (s1, socket_address);
     s2 = test_socket (AF_SP, NN_PAIR);
-    test_connect (s2, SOCKET_ADDRESS);
+    test_connect (s2, socket_address);
     nn_sleep (100);
     test_close (s2);
     test_close (s1);
@@ -166,11 +172,11 @@
 
     /*  Test two sockets binding to the same address. */
     sb = test_socket (AF_SP, NN_PAIR);
-    test_bind (sb, SOCKET_ADDRESS);
+    test_bind (sb, socket_address);
     s1 = test_socket (AF_SP, NN_PAIR);
-    test_bind (s1, SOCKET_ADDRESS);
+    test_bind (s1, socket_address);
     sc = test_socket (AF_SP, NN_PAIR);
-    test_connect (sc, SOCKET_ADDRESS);
+    test_connect (sc, socket_address);
     nn_sleep (100);
     test_send (sb, "ABC");
     test_recv (sc, "ABC");
@@ -182,9 +188,9 @@
 
     /*  Test NN_RCVMAXSIZE limit */
     sb = test_socket (AF_SP, NN_PAIR);
-    test_bind (sb, SOCKET_ADDRESS);
+    test_bind (sb, socket_address);
     s1 = test_socket (AF_SP, NN_PAIR);
-    test_connect (s1, SOCKET_ADDRESS);
+    test_connect (s1, socket_address);
     opt = 4;
     rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt));
     nn_assert (rc == 0);
@@ -211,12 +217,12 @@
 
     /*  Test closing a socket that is waiting to bind. */
     sb = test_socket (AF_SP, NN_PAIR);
-    test_bind (sb, SOCKET_ADDRESS);
+    test_bind (sb, socket_address);
     nn_sleep (100);
     s1 = test_socket (AF_SP, NN_PAIR);
-    test_bind (s1, SOCKET_ADDRESS);
+    test_bind (s1, socket_address);
     sc = test_socket (AF_SP, NN_PAIR);
-    test_connect (sc, SOCKET_ADDRESS);
+    test_connect (sc, socket_address);
     nn_sleep (100);
     test_send (sb, "ABC");
     test_recv (sc, "ABC");
diff --git a/tests/tcp_shutdown.c b/tests/tcp_shutdown.c
index fa58d44..012c111 100644
--- a/tests/tcp_shutdown.c
+++ b/tests/tcp_shutdown.c
@@ -1,5 +1,6 @@
 /*
     Copyright (c) 2012 Martin Sustrik  All rights reserved.
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -37,10 +38,11 @@
 #define TEST2_THREAD_COUNT 10
 #define MESSAGES_PER_THREAD 10
 #define TEST_LOOPS 10
-#define SOCKET_ADDRESS "tcp://127.0.0.1:5557"
 
 struct nn_atomic active;
 
+static char socket_address[128];
+
 static void routine (NN_UNUSED void *arg)
 {
     int s;
@@ -49,7 +51,7 @@
     if (s < 0 && nn_errno () == EMFILE)
         return;
     errno_assert (s >= 0);
-    test_connect (s, SOCKET_ADDRESS);
+    test_connect (s, socket_address);
     test_close (s);
 }
 
@@ -62,7 +64,7 @@
     s = test_socket (AF_SP, NN_PULL);
 
     for (i = 0; i < 10; ++i) {
-        test_connect (s, SOCKET_ADDRESS);
+        test_connect (s, socket_address);
     }
 
     ms = 2000;
@@ -76,13 +78,16 @@
     nn_atomic_dec(&active, 1);
 }
 
-int main ()
+int main (int argc, const char *argv[])
 {
     int sb;
     int i;
     int j;
     struct nn_thread threads [THREAD_COUNT];
 
+    test_addr_from(socket_address, "tcp", "127.0.0.1",
+            get_test_port(argc, argv));
+
     /*  Stress the shutdown algorithm. */
 
 #if defined(SIGPIPE) && defined(SIG_IGN)
@@ -90,7 +95,7 @@
 #endif
 
     sb = test_socket (AF_SP, NN_PUB);
-    test_bind (sb, SOCKET_ADDRESS);
+    test_bind (sb, socket_address);
 
     for (j = 0; j != TEST_LOOPS; ++j) {
         for (i = 0; i != THREAD_COUNT; ++i)
@@ -105,7 +110,7 @@
     /*  Test race condition of sending message while socket shutting down  */
 
     sb = test_socket (AF_SP, NN_PUSH);
-    test_bind (sb, SOCKET_ADDRESS);
+    test_bind (sb, socket_address);
 
     for (j = 0; j != TEST_LOOPS; ++j) {
 	int ms;
diff --git a/tests/testutil.h b/tests/testutil.h
index d6c1108..638f926 100644
--- a/tests/testutil.h
+++ b/tests/testutil.h
@@ -1,6 +1,7 @@
 /*
     Copyright (c) 2013 Insollo Entertainment, LLC. All rights reserved.
     Copyright 2015 Garrett D'Amore <garrett@damore.org>
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -200,4 +201,15 @@
     }
 }
 
+static int get_test_port(int argc, const char *argv[])
+{
+    return atoi(argc < 2 ? "5555" : argv[1]);
+}
+
+static void test_addr_from(char *out, const char *proto,
+        const char *ip, int port)
+{
+    sprintf(out, "%s://%s:%d", proto, ip, port);
+}
+
 #endif
diff --git a/tests/ws.c b/tests/ws.c
index 4142576..1766743 100644
--- a/tests/ws.c
+++ b/tests/ws.c
@@ -2,6 +2,7 @@
     Copyright (c) 2012 250bpm s.r.o.  All rights reserved.
     Copyright (c) 2014 Wirebird Labs LLC.  All rights reserved.
     Copyright 2015 Garrett D'Amore <garrett@damore.org>
+    Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"),
@@ -28,7 +29,7 @@
 
 #include "testutil.h"
 
-#define SOCKET_ADDRESS  "ws://127.0.0.1:5555"
+static char socket_address[128];
 
 /*  Basic tests for WebSocket transport. */
 
@@ -55,8 +56,8 @@
     opt = 500;
     test_setsockopt(sb, NN_SOL_SOCKET, NN_RCVTIMEO, &opt, sizeof (opt));
 
-    test_bind (sb, SOCKET_ADDRESS);
-    test_connect (sc, SOCKET_ADDRESS);
+    test_bind (sb, socket_address);
+    test_connect (sc, socket_address);
 
     test_send (sc, "GOOD");
     test_recv (sb, "GOOD");
@@ -70,7 +71,7 @@
     test_drop (sb, ETIMEDOUT);
 }
 
-int main ()
+int main (int argc, const char *argv[])
 {
     int rc;
     int sb;
@@ -78,16 +79,23 @@
     int opt;
     size_t sz;
     int i;
+    char any_address[128];
+
+    test_addr_from(socket_address, "ws", "127.0.0.1",
+            get_test_port(argc, argv));
+
+    test_addr_from(any_address, "ws", "*",
+            get_test_port(argc, argv));
 
     /*  Try closing bound but unconnected socket. */
     sb = test_socket (AF_SP, NN_PAIR);
-    test_bind (sb, "ws://*:5555");
+    test_bind (sb, any_address);
     test_close (sb);
 
     /*  Try closing a TCP socket while it not connected. At the same time
         test specifying the local address for the connection. */
     sc = test_socket (AF_SP, NN_PAIR);
-    test_connect (sc, "ws://127.0.0.1:5555");
+    test_connect (sc, socket_address);
     test_close (sc);
 
     /*  Open the socket anew. */
@@ -163,9 +171,9 @@
     nn_sleep (200);
 
     sb = test_socket (AF_SP, NN_PAIR);
-    test_bind (sb, SOCKET_ADDRESS);
+    test_bind (sb, socket_address);
     sc = test_socket (AF_SP, NN_PAIR);
-    test_connect (sc, SOCKET_ADDRESS);
+    test_connect (sc, socket_address);
 
     /*  Leave enough time for connection establishment. */
     nn_sleep (200);