diff --git a/configure.ac b/configure.ac
index b34def561aace78752e8191f132a3556ae0da6d5..b5c6f11ca4f677cb11b220ef1fc974474ec7f70d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -42,11 +42,12 @@ AC_SEARCH_LIBS([pthread_create], [pthread], [], [AC_MSG_ERROR([pthreads not foun
 AC_SEARCH_LIBS([rcu_read_lock], [urcu], [], [AC_MSG_ERROR([liburcu not found])])
 AC_SEARCH_LIBS([clock_gettime], [rt], [], [AC_MSG_ERROR([librt not found])])
 AC_SEARCH_LIBS([OpenSSL_add_all_digests], [crypto],[], [AC_MSG_ERROR([libcrypto not found])])
+AC_SEARCH_LIBS([ev_loop], [ev],[], [AC_MSG_ERROR([libev not found])])
 #AC_SEARCH_LIBS([ldns_rr_list_pop_rrset], [ldns], [], [AC_MSG_ERROR([libldns not found])])
 
 # Checks for header files.
 AC_HEADER_RESOLV
-AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h limits.h malloc.h netdb.h netinet/in.h stdint.h stdlib.h string.h strings.h sys/socket.h sys/time.h syslog.h unistd.h urcu.h])
+AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h limits.h malloc.h netdb.h netinet/in.h stdint.h stdlib.h string.h strings.h sys/socket.h sys/time.h syslog.h unistd.h urcu.h ev.h])
 
 # Checks for typedefs, structures, and compiler characteristics.
 AC_HEADER_STDBOOL
diff --git a/resource.sh b/resource.sh
index 9c3bc8a8d4d8486a4e2ffdc501632ee84f6170d7..bb5c065a30da377cfa8f885fc94351c81d7e9423 100755
--- a/resource.sh
+++ b/resource.sh
@@ -19,7 +19,7 @@ header="${1%.*}_rc"
 header=`basename ${header}`
 
 # Get file size and dump content
-size=`stat -c %s ${1} 2>/dev/null`
+size=`stat -f "%z" ${1} 2>/dev/null`
 dump=`${hd} "${fmt}" ${1} 2>/dev/null`
 
 # Format file size variable
diff --git a/src/config.h.in b/src/config.h.in
index 57b887492cbaa0af6950b700535f7e0b658362b9..017aba0986653cb0646ca588c9ba274767367b29 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -9,6 +9,9 @@
 /* Define to 1 if you have the <dlfcn.h> header file. */
 #undef HAVE_DLFCN_H
 
+/* Define to 1 if you have the <ev.h> header file. */
+#undef HAVE_EV_H
+
 /* Define to 1 if you have the <fcntl.h> header file. */
 #undef HAVE_FCNTL_H
 
diff --git a/src/dnslib/dnslib-common.h b/src/dnslib/dnslib-common.h
index 8b65dec84020bd004cf9edbb997782000cf1c75b..f8a069a831cfe64096f7b182f2cdc3dbf788c8e8 100644
--- a/src/dnslib/dnslib-common.h
+++ b/src/dnslib/dnslib-common.h
@@ -20,7 +20,10 @@
 #define DNSLIB_NAME "dnslib" // Project name
 #define DNSLIB_VER  0x000100  // 0xMMIIRR (MAJOR,MINOR,REVISION)
 
-typedef unsigned int uint;
+#ifndef UINT_DEFINED
+typedef unsigned int uint; /*!< \brief Unsigned. */
+#define UINT_DEFINED
+#endif
 
 /*! \brief If defined, zone structures will use hash table for lookup. */
 #define USE_HASH_TABLE
diff --git a/src/knot/common.h b/src/knot/common.h
index 9ea1b02d4779c5fa14f86dd995454008a7e48f64..b0a5192a6a6811b50af9b275b875fb9284cfb3c5 100644
--- a/src/knot/common.h
+++ b/src/knot/common.h
@@ -20,7 +20,10 @@
  * Common types and constants.
  */
 
+#ifndef UINT_DEFINED
 typedef unsigned int uint; /*!< \brief Unsigned. */
+#define UINT_DEFINED
+#endif
 
 #define PROJECT_NAME PACKAGE /*!< \brief Project name. */
 #define PROJECT_VER  0x000100  /*!< \brief  0xMMIIRR (MAJOR,MINOR,REVISION). */
diff --git a/src/knot/server/axfr-in.h b/src/knot/server/axfr-in.h
index 8f947acf8f7f9b2b74236fc74fd1c387d8e8b8f6..ee6a68129800582e5494907aae54047822927855 100644
--- a/src/knot/server/axfr-in.h
+++ b/src/knot/server/axfr-in.h
@@ -19,6 +19,7 @@
 #include "dnslib/zone.h"
 #include "dnslib/packet.h"
 
+
 int axfrin_create_soa_query(const dnslib_dname_t *zone_name, uint8_t *buffer,
                             size_t *size);
 
diff --git a/src/knot/server/server.c b/src/knot/server/server.c
index 4913414d1d9bb0700dcf9059b640dedb2c18d03b..b80dd072e61f5a5173cd9adde12b1f0d93b6bd2f 100644
--- a/src/knot/server/server.c
+++ b/src/knot/server/server.c
@@ -119,8 +119,9 @@ static int server_init_iface(iface_t *new_if, conf_iface_t *cfg_if)
 	/* Create UDP socket. */
 	int sock = socket_create(cfg_if->family, SOCK_DGRAM);
 	if (sock <= 0) {
+		strerror_r(errno, errbuf, sizeof(errbuf));
 		log_server_error("Could not create UDP socket: %s.\n",
-				 strerror_r(errno, errbuf, sizeof(errbuf)));
+				 errbuf);
 		return sock;
 	}
 	if (socket_bind(sock, cfg_if->family,
@@ -149,8 +150,9 @@ static int server_init_iface(iface_t *new_if, conf_iface_t *cfg_if)
 	sock = socket_create(cfg_if->family, SOCK_STREAM);
 	if (sock <= 0) {
 		socket_close(new_if->fd[UDP_ID]);
+		strerror_r(errno, errbuf, sizeof(errbuf));
 		log_server_error("Could not create TCP socket: %s.\n",
-		                 strerror_r(errno, errbuf, sizeof(errbuf)));
+				 errbuf);
 		return sock;
 	}
 
diff --git a/src/knot/server/tcp-handler.c b/src/knot/server/tcp-handler.c
index 0ef6cfcb1ca2200e3754dfec0a1400e72ac6d71f..ccf8fca8c366c6605d9822edca3a41304055d812 100644
--- a/src/knot/server/tcp-handler.c
+++ b/src/knot/server/tcp-handler.c
@@ -5,11 +5,12 @@
 #include <netinet/tcp.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
-#include <sys/epoll.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 
+#include <ev.h>
+
 #include "common/sockaddr.h"
 #include "knot/common.h"
 #include "knot/server/tcp-handler.h"
@@ -19,33 +20,18 @@
 #include "knot/stat/stat.h"
 #include "dnslib/wire.h"
 
-/*! \brief TCP connection pool. */
-typedef struct tcp_pool_t {
-	int                epfd;       /*!< Epoll socket. */
-	int                evcount;    /*!< Epoll events counter */
-	struct epoll_event *events;    /*!< Epoll events backing store. */
-	int                ebs_size;   /*!< Epoll events backing store size. */
-	pthread_mutex_t    mx;         /*!< Pool synchronisation lock. */
+/*! \brief TCP connection. */
+typedef struct tcp_io_t {
+	ev_io io;
 	ns_nameserver_t    *ns;        /* reference to name server */
 	iohandler_t        *io_h;      /* master I/O handler */
 	xfrhandler_t       *xfr_h;     /* XFR handler */
 	stat_t             *stat;      /* statistics gatherer */
-} tcp_pool_t;
+} tcp_io_t;
 
 /*
  * Forward decls.
  */
-/*! \brief Lock TCP pool. */
-static inline int tcp_pool_lock(tcp_pool_t *pool)
-{
-	return pthread_mutex_lock(&pool->mx);
-}
-
-/*! \brief Unlock TCP pool. */
-static inline int tcp_pool_unlock(tcp_pool_t *pool)
-{
-	return pthread_mutex_unlock(&pool->mx);
-}
 
 /*!
  * \brief Send TCP message.
@@ -137,6 +123,7 @@ static inline int tcp_recv(int fd, uint8_t *buf, size_t len, sockaddr_t *addr)
 	return n;
 }
 
+#if 0
 /*!
  * \brief TCP event handler function.
  *
@@ -239,38 +226,6 @@ static inline int tcp_handle(tcp_pool_t *pool, int fd,
 	return res;
 }
 
-/*!
- * \brief Reserve backing store for a given number of sockets.
- *
- * \param pool Given TCP pool instance.
- * \param size Minimum requested backing store size.
- * \retval 0 on success.
- * \retval <0 on error.
- */
-static int tcp_pool_reserve(tcp_pool_t *pool, uint size)
-{
-	if (pool->ebs_size >= size) {
-		return 0;
-	}
-
-	// Alloc new events
-	struct epoll_event *new_events =
-	                malloc(size * sizeof(struct epoll_event));
-
-	if (new_events == 0) {
-		return -1;
-	}
-
-	// Free and replace old events backing-store
-	if (pool->events != 0) {
-		free(pool->events);
-	}
-
-	pool->ebs_size = size;
-	pool->events = new_events;
-	return 0;
-}
-
 /*!
  * \brief Create new TCP pool.
  *
@@ -508,6 +463,12 @@ static int tcp_pool(dthread_t *thread)
 	debug_net("tcp: pool #%d going to idle.\n", pool->epfd);
 	return 0;
 }
+#endif
+
+static void tcp_accept(EV_P_ ev_io *w, int revents)
+{
+	tcp_io_t *tcp_w = (tcp_io_t *)w;
+}
 
 /*
  * Public APIs.
@@ -528,17 +489,18 @@ int tcp_master(dthread_t *thread)
 	debug_dt("dthreads: [%p] is TCP master, state: %d\n",
 	         thread, thread->state);
 
-	/*
-	 * Create N pools of TCP connections.
-	 * Each pool is responsible for its own
-	 * set of clients.
-	 *
-	 * Pool instance is deallocated by their assigned thread.
-	 */
-	int pool_id = -1;
+	/* Create event loop. */
+	struct ev_loop *loop = ev_default_loop(0);
 
-	// Accept clients
+	/* Watch bound socket for incoming connections. */
+	tcp_io_t tcp_w;
+	ev_io_init((ev_io *)&tcp_w, tcp_accept, master_sock, EV_READ);
+	ev_io_start(loop, (ev_io *)&tcp_w);
+
+	/* Accept clients. */
 	debug_net("tcp: running 1 master with %d pools\n", unit->size - 1);
+
+
 	for (;;) {
 
 		// Cancellation point
@@ -548,6 +510,10 @@ int tcp_master(dthread_t *thread)
 			return KNOT_EOK;
 		}
 
+		/*! \bug Implement cancellation point somehow. */
+		ev_loop(loop, 0);
+	}
+#if 0
 		// Accept on master socket
 		int incoming = accept(master_sock, 0, 0);
 
@@ -589,7 +555,7 @@ int tcp_master(dthread_t *thread)
 			dt_unit_unlock(unit);
 		}
 	}
-
+#endif
 
 	// Stop whole unit
 	debug_net("tcp: stopping (%d master, %d pools)\n", 1, unit->size - 1);
diff --git a/src/knot/server/xfr-handler.c b/src/knot/server/xfr-handler.c
index f498db274ae5875c0aceb52247cdf3e0078f8893..3f1bcf4604729cb31ff872d2baeb03c9386b0a71 100644
--- a/src/knot/server/xfr-handler.c
+++ b/src/knot/server/xfr-handler.c
@@ -5,7 +5,6 @@
 #include <netinet/tcp.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
-#include <sys/epoll.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/src/tests/common/acl_tests.c b/src/tests/common/acl_tests.c
index d5a7cd8e03e03cce0cf7f1527ca42bf11cee3ac3..d17f8523387b657725cecd161162095339628be2 100644
--- a/src/tests/common/acl_tests.c
+++ b/src/tests/common/acl_tests.c
@@ -1,3 +1,5 @@
+#include <sys/socket.h>
+
 #include "tests/common/acl_tests.h"
 #include "common/sockaddr.h"
 #include "common/acl.h"
diff --git a/src/tests/knot/dthreads_tests.c b/src/tests/knot/dthreads_tests.c
index efcc598abf835bd4b83bbf3a62f8c8ac7da9a830..d4ca47b7f836dd66cd578f3a828e56442e0661fa 100644
--- a/src/tests/knot/dthreads_tests.c
+++ b/src/tests/knot/dthreads_tests.c
@@ -1,6 +1,7 @@
 #include <pthread.h>
 #include <sched.h>
 #include <sys/select.h>
+#include <signal.h>
 
 #include "tests/knot/dthreads_tests.h"
 #include "knot/server/dthreads.h"
diff --git a/src/zcompile/parser-util.c b/src/zcompile/parser-util.c
index ad79a8eff65920ec0845b22fcd5b70689b02fee8..9fb88ee84c25f1beb49607e2919736cab0e4471a 100644
--- a/src/zcompile/parser-util.c
+++ b/src/zcompile/parser-util.c
@@ -23,6 +23,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <netinet/in.h>
+#include <sys/socket.h>
 #include <netdb.h>
 
 //#include "common.h"
diff --git a/src/zcompile/zcompile.c b/src/zcompile/zcompile.c
index 18b26c459fc9d17177ca1b83f5a6e89cf169856a..6030b10ee6ea8552ede4f46412b674989f2bee36 100644
--- a/src/zcompile/zcompile.c
+++ b/src/zcompile/zcompile.c
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <netinet/in.h>
+#include <sys/socket.h>
 #include <netdb.h>
 #include <assert.h>