Index: /trunk/include/cf_debug.h
===================================================================
--- /trunk/include/cf_debug.h	(revision 20)
+++ /trunk/include/cf_debug.h	(revision 21)
@@ -33,5 +33,5 @@
 	do {															\
 	CF_UPDATE_CTX (__ctx);											\
-	CF_Debug_Trace (__ctx,__bin,__len,__fmt,##__VA_ARGS__);			\
+	CF_Debug_TraceBin (__ctx,__bin,__len,__fmt,##__VA_ARGS__);		\
 	} while (0)
 # define CF_DEBUG_CALLSTACK_PUSH(__ctx)								\
Index: /trunk/include/cf_socket.h
===================================================================
--- /trunk/include/cf_socket.h	(revision 20)
+++ /trunk/include/cf_socket.h	(revision 21)
@@ -7,4 +7,14 @@
 #include "cf_base.h"
 #include <stddef.h>
+
+#ifdef _WIN32
+# include <WinSock2.h>
+#else
+# include <netinet/in.h>
+# include <sys/socket.h>
+# include <arpa/inet.h>
+# include <netdb.h>
+# include <unistd.h>
+#endif
 
 #define CF_ERROR_SOCKET_INITIALIZE		CF_ERROR_SOCKET - 1
@@ -71,6 +81,6 @@
 
 CF_EXPORT int
-CF_Socket_Accept		(const int	sock,
-						 void		* address /* struct sockaddr_in * */);
+CF_Socket_Accept		(const int			sock,
+						 struct sockaddr_in	* address);
 
 CF_EXPORT int
Index: /trunk/src/cf_socket.c
===================================================================
--- /trunk/src/cf_socket.c	(revision 20)
+++ /trunk/src/cf_socket.c	(revision 21)
@@ -6,14 +6,8 @@
 
 #ifdef _WIN32
-# include <WinSock2.h>
 # pragma comment (lib, "ws2_32.lib")
 # define close(__sock)	closesocket(__sock)
 # define sa_family_t	unsigned short
 #else
-# include <netinet/in.h>
-# include <sys/socket.h>
-# include <arpa/inet.h>
-# include <netdb.h>
-# include <unistd.h>
 #endif
 
@@ -216,5 +210,5 @@
 	}
 
-	return CF_OK;
+	return sock;
 }
 
@@ -262,6 +256,6 @@
 
 int
-CF_Socket_Accept (const int	sock,
-				  void		* address)
+CF_Socket_Accept (const int				sock,
+				  struct sockaddr_in	* address)
 {
 	int					sockClient;
Index: /trunk/src/cf_thread.c
===================================================================
--- /trunk/src/cf_thread.c	(revision 20)
+++ /trunk/src/cf_thread.c	(revision 21)
@@ -27,5 +27,5 @@
 
 int
-CF_CLOSE_CTX (void * ctx)
+CF_Thread_Local_Close (void * ctx)
 {
 #ifdef _WIN32
@@ -67,5 +67,5 @@
 	CHECK_INVALID_THREAD (*threadID);
 
-	CF_CLOSE_CTX (*threadID);
+	CF_Thread_Local_Close (*threadID);
 
 	return CF_OK;
@@ -80,6 +80,6 @@
 	WaitForSingleObject ((THREAD_TYPE) *threadID, INFINITE);
 #else
-	int status;
-	pthread_join (*((THREAD_TYPE *) *threadID), (void *)&status);
+	int status = 0;
+	pthread_join (*((THREAD_TYPE *) *threadID), &status);
 #endif
 
@@ -114,5 +114,5 @@
 	CHECK_INVALID_MUTEX (*mutex);
 
-	CF_CLOSE_CTX (*mutex);
+	CF_Thread_Local_Close (*mutex);
 
 	return CF_OK;
Index: /trunk/test/test.c
===================================================================
--- /trunk/test/test.c	(revision 20)
+++ /trunk/test/test.c	(revision 21)
@@ -9,8 +9,15 @@
 
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#define THREAD_POOL	5
+#define PORT		1234
 
 CF_Debug_Ctx		gDebugCtx;
 CF_Debug_CallStack	gDebugCallstack;
 
+CF_Log_Ctx			gLogCtx;
+
 const char * file = "./log.txt";
 
@@ -19,10 +26,11 @@
 	int			i, j;
 	char		idname[16] = {0x00,};
-	CF_Log_Ctx	gLogCtx;
-
-	CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
-
+
+	CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
+
+	/* initialize */
 	CF_Log_Initialize (10);
 
+	/* context {{{ */
 	gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT);
 	if (gLogCtx == NULL)
@@ -41,7 +49,7 @@
 
 	CF_Log_DestroyCtx (gLogCtx);
-
-	///////////////////
-
+	/* }}} context */
+
+	/* id {{{ */
 	for (i = 0 ; i < 10 ; i++)
 	{
@@ -57,5 +65,7 @@
 		CF_LOG_CLOSE (i);
 	}
-
+	/* }}} id */
+
+	/* finalize */
 	CF_Log_Finalize ();
 }
@@ -63,7 +73,6 @@
 void file_test (const char * message)
 {
-	int		size = 0;
 	int		fd = 0;
-	char	* buffer = NULL;
+	char	buffer[128] = {0x00,};
 
 	CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
@@ -73,16 +82,12 @@
 		CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
 
-	size = CF_File_GetSize (fd);
-	if (size < 0)
-		CF_DEBUG_TRACE (gDebugCtx, "what the ... file size ?\n");
-
-	buffer = (char *) calloc (1, (size_t) (size + 1));
-	if (buffer == NULL)
-		CF_DEBUG_TRACE (gDebugCtx, "what the ... buffer ?\n");
-
-	if (CF_File_Read (fd, buffer, (size_t)size) < 0)
+	if (CF_File_Read (fd, buffer, sizeof (buffer) - 1) < 0)
 		CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n");
 	else
-		CF_DEBUG_TRACE (gDebugCtx, "%s", buffer);
+		CF_DEBUG_TRACE_BIN (gDebugCtx, (unsigned char *)buffer,
+										sizeof (buffer) - 1,
+										"-- %d bytes of %d bytes\n",
+										sizeof (buffer) - 1,
+										CF_File_GetSize (fd));
 
 	CF_File_Close (fd);
@@ -116,4 +121,159 @@
 }
 
+CF_THREAD_RETURN CF_THREAD_CALL socket_echo_server (void * arg)
+{
+	int srvsock = *((int *)arg);
+	int clntsock = 0;
+	int recvd = 0;
+	char buf[1024] = {0x00,};
+
+	/*------------------------------------------------------------*/
+	clntsock = CF_Socket_Accept (srvsock, NULL);
+	if (clntsock < 0)
+	{
+		CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
+		return (CF_THREAD_RETURN)-1;
+	}
+	CF_DEBUG_PRINT (stderr, "SERVER : accepted\n");
+	CF_Log_Write (gLogCtx, "SERVER", "accepted\n");
+
+	if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
+	{
+		CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
+		return (CF_THREAD_RETURN)-2;
+	}
+	CF_DEBUG_PRINT (stderr, "SERVER : recv {%s}\n", buf);
+	CF_Log_Write (gLogCtx, "SERVER", "recv {%s}\n", buf);
+
+	if (CF_Socket_Send (clntsock, buf, recvd) < 0)
+	{
+		CF_DEBUG_PRINT (stderr, "failed to send on server\n");
+		return (CF_THREAD_RETURN)-3;
+	}
+	CF_DEBUG_PRINT (stderr, "SERVER : resp {%s}\n", buf);
+	CF_Log_Write (gLogCtx, "SERVER", "resp {%s}\n", buf);
+
+	return (CF_THREAD_RETURN)0;
+	/*------------------------------------------------------------*/
+}
+
+CF_THREAD_RETURN CF_THREAD_CALL socket_echo_client (void * arg)
+{
+	int sock = 0;
+	int recvd = 0;
+	char buf[1024] = {0x00,};
+
+	sprintf (buf, "...wow ? is it succeed ?");
+
+	/*------------------------------------------------------------*/
+	sock = CF_Socket_Connect ("localhost", PORT);
+	if (sock < 0)
+	{
+		CF_DEBUG_PRINT (stderr, "failed to connect on client\n");
+		return (CF_THREAD_RETURN)-1;
+	}
+	CF_DEBUG_PRINT (stderr, "CLIENT : connected\n");
+	CF_Log_Write (gLogCtx, "CLIENT", "connected\n");
+
+	if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
+	{
+		CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
+		return (CF_THREAD_RETURN)-2;
+	}
+	CF_DEBUG_PRINT (stderr, "CLIENT : sent {%s}\n", buf);
+	CF_Log_Write (gLogCtx, "CLIENT", "sent {%s}\n", buf);
+
+	memset (buf, 0x00, sizeof (buf));
+
+	if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
+	{
+		CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
+		return (CF_THREAD_RETURN)-3;
+	}
+	CF_DEBUG_PRINT (stderr, "CLIENT : recv {%s}\n", buf);
+	CF_Log_Write (gLogCtx, "CLIENT", "recv {%s}\n", buf);
+
+	CF_Socket_Close (sock);
+
+	return (CF_THREAD_RETURN)0;
+	/*------------------------------------------------------------*/
+}
+
+void socket_test (void)
+{
+	CF_Thread tid[THREAD_POOL];
+
+	int sock = 0;
+	int iter = 0;
+	int reuse = 1;
+
+	/*------------------------------------------------------------*/
+	gLogCtx = CF_Log_CreateCtx ("socket.txt", CF_LOG_BUFFER_DEFAULT);
+	if (gLogCtx == NULL)
+		CF_DEBUG_PRINT (stderr, "failed to open log\n");
+
+	CF_Log_Write (gLogCtx, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
+
+	if (CF_Socket_Initialize () < 0)
+	{
+		CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
+		return ;
+	}
+	CF_Log_Write (gLogCtx, "SOCKET", "socket initialized\n");
+
+	sock = CF_Socket_Server (PORT, 5);
+	if (sock < 0)
+	{
+		CF_DEBUG_PRINT (stderr, "failed to ready server %d\n", sock);
+		return ;
+	}
+	CF_Log_Write (gLogCtx, "SOCKET", "socket ready\n");
+
+	if (CF_Socket_SetOption (sock, SO_REUSEADDR, &reuse, sizeof (reuse)) < 0)
+	{
+		CF_DEBUG_PRINT (stderr, "failed to set option\n");
+		return ;
+	}
+	CF_Log_Write (gLogCtx, "SOCKET", "set socket option\n");
+
+	for (iter = 0 ; iter < THREAD_POOL ; iter++)
+	{
+		if (CF_Thread_Create (&tid[iter], socket_echo_server, &sock) < 0)
+		{
+			CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
+			return ;
+		}
+		CF_Log_Write (gLogCtx, "SOCKET", "create server thread-%d\n", iter);
+	}
+
+	for (iter = 0 ; iter < THREAD_POOL ; iter++)
+	{
+		CF_Thread dummy;
+		if (CF_Thread_Create (&dummy, socket_echo_client, &sock) < 0)
+		{
+			CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
+			return ;
+		}
+		CF_Log_Write (gLogCtx, "SOCKET", "create client thread-%d\n", iter);
+	}
+
+	for (iter = 0 ; iter < THREAD_POOL ; iter++)
+	{
+		int st;
+		CF_DEBUG_PRINT (stderr, "tid[%d] 0x%08x\n", iter, tid[iter]);
+		CF_Thread_Join (&tid[iter]);
+//		pthread_join (*((pthread_t *)*((CF_Thread *)&tid[iter])), &st);
+		CF_Thread_Release (&tid[iter]);
+		CF_Log_Write (gLogCtx, "SOCKET", "join server thread-%d\n", iter);
+	}
+
+	CF_Socket_Close (sock);
+
+	CF_Socket_Finalize ();
+
+	CF_Log_DestroyCtx (gLogCtx);
+	/*------------------------------------------------------------*/
+}
+
 int main (int argc, char ** argv)
 {
@@ -122,12 +282,19 @@
 	CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
 
+	// 1
 	CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
 	log_test ("LOG_TEST");
 
+	// 2
 	CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
 	file_test ("FILE_READ_TEST");
 
+	// 3
 	CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
 	callee1 ();
+
+	// 4
+	CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST ==\n");
+	socket_test ();
 
 	CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
@@ -143,2 +310,3 @@
 	return 0;
 }
+
