Index: /trunk/include/CRXException.h
===================================================================
--- /trunk/include/CRXException.h	(revision 60)
+++ /trunk/include/CRXException.h	(revision 61)
@@ -59,4 +59,7 @@
 	} while (0)
 
+
+#define CRX_SYSTEM_ERROR()	CRXException::GetSystemErrorCode ()
+
 class CRXException
 {
@@ -70,10 +73,11 @@
 
 public:
+	static int	GetSystemErrorCode		(void);
+
+public:
 	/* get */
 	int			GetErrorCode			(void) const;
 
 	std::string	GetErrorMessage			(void) const;
-
-	int			GetSystemErrorCode		(void) const;
 
 	/* set */
Index: /trunk/include/CRXSocket.h
===================================================================
--- /trunk/include/CRXSocket.h	(revision 60)
+++ /trunk/include/CRXSocket.h	(revision 61)
@@ -36,9 +36,9 @@
 #define ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT	ERROR_TCPSOCKET - 12
 #define ERROR_TCPSOCKET_FAILED_TO_SET_TIMEOUT	ERROR_TCPSOCKET - 13
+#define ERROR_TCPSOCKET_FAILED_TO_GET_SOCKOPT	ERROR_TCPSOCKET - 14
 
 class CRXSocket : public CRXException
 {
 private:
-	static bool			mInitialized;
 	struct sockaddr_in	mAddress;
 	int					mSocket;
@@ -73,5 +73,5 @@
 	 * common functions
 	 */
-	CRXSocket					(int aSocket = 0);
+	CRXSocket					(int aSocket = -1);
 
 	~CRXSocket					(void);
@@ -86,4 +86,8 @@
 
 	CRXSocket	& operator =	(int aSocket);
+
+	int			GetOption		(const int	aOptName,
+								 void		* aOptVal,
+								 size_t		* aOptLen);
 
 	int			SetOption		(const int		aOptName,
Index: /trunk/src/CRXException.cpp
===================================================================
--- /trunk/src/CRXException.cpp	(revision 60)
+++ /trunk/src/CRXException.cpp	(revision 61)
@@ -43,5 +43,5 @@
 
 int
-CRXException::GetSystemErrorCode (void) const
+CRXException::GetSystemErrorCode (void)
 {
 	/*----------------------------------------------------------------*/
Index: /trunk/src/CRXProxy.cpp
===================================================================
--- /trunk/src/CRXProxy.cpp	(revision 60)
+++ /trunk/src/CRXProxy.cpp	(revision 61)
@@ -96,5 +96,5 @@
 	{
 		aResult = mClient.Receive (&aBuffer, 1);
-		if (aResult < 0)
+		if (aResult == -1)
 		{
 			aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
@@ -159,5 +159,5 @@
 			CRX_ADD_SUBCLASS_ERROR (mServer);
 			CRX_ERROR_SET (aResult, "Failed to receive from server.");
-			return aResult;
+			break;
 		}
 		else if (aResult == 0)
@@ -184,5 +184,5 @@
 				aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
 				CRX_ERROR_SET (aResult, "Failed to set response.");
-				return aResult;
+				break;
 			}
 			else if (aResult == 0)
@@ -200,4 +200,5 @@
 	int	aResult = 0;
 	int	aLength = 0;
+
 	/*----------------------------------------------------------------*/
 	aLength = mHttpResponse.GetHeader ().length ();
Index: /trunk/src/CRXSocket.cpp
===================================================================
--- /trunk/src/CRXSocket.cpp	(revision 60)
+++ /trunk/src/CRXSocket.cpp	(revision 61)
@@ -11,8 +11,7 @@
 #endif
 
-bool CRXSocket::mInitialized = false;
+bool gInitialized = false;
 
 CRXSocket::CRXSocket (int aSocket)
-	: mSocket (aSocket)
 {
 	/*----------------------------------------------------------------*/
@@ -20,4 +19,6 @@
 
 	memset ((void *)&mAddress, 0x0, sizeof (struct sockaddr_in));
+
+	Attach (aSocket);
 	/*----------------------------------------------------------------*/
 }
@@ -33,6 +34,36 @@
 CRXSocket::Attach (int aSocket)
 {
+	int		aResult = 0;
+	int		aReuseAddress = 0;
+	size_t	aReuseAddressLength = sizeof (aReuseAddress);
+
 	/*----------------------------------------------------------------*/
 	mSocket = aSocket;
+
+	if (mSocket < 0)
+		return ;
+
+	aResult = GetOption (SO_REUSEADDR,
+						 &aReuseAddress,
+						 &aReuseAddressLength);
+	if (aResult < 0)
+	{
+		aResult = ERROR_TCPSOCKET_FAILED_TO_GET_SOCKOPT;
+		CRX_ERROR_SET (aResult, "Failed to get socket option (reuseaddr).(%d)", mSocket);
+	}
+
+	if (aReuseAddress == 1)
+		return ;
+
+	aReuseAddress = 1;
+
+	aResult = SetOption (SO_REUSEADDR,
+						 &aReuseAddress,
+						 sizeof (aReuseAddress));
+	if (aResult < 0)
+	{
+		aResult = ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT;
+		CRX_ERROR_SET (aResult, "Failed to set socket option (reuseaddr).(%d)", mSocket);
+	}
 	/*----------------------------------------------------------------*/
 }
@@ -45,5 +76,4 @@
 	/*----------------------------------------------------------------*/
 	mSocket = 0;
-
 	/*----------------------------------------------------------------*/
 	return aSocket;
@@ -76,4 +106,31 @@
 	/*----------------------------------------------------------------*/
 	return *this;
+}
+
+int
+CRXSocket::GetOption (const int	aOptName,
+					  void		* aOptVal,
+					  size_t	* aOptLen)
+{
+	int	aResult = 0;
+
+	/*----------------------------------------------------------------*/
+	aResult = getsockopt (mSocket,
+						  SOL_SOCKET,
+						  aOptName,
+#ifdef _WIN32
+						  (char *) aOptVal,
+#else
+						  aOptVal,
+#endif
+						  (socklen_t *) aOptLen);
+	if (aResult < 0)
+	{
+		aResult = ERROR_TCPSOCKET_FAILED_TO_GET_SOCKOPT;
+		CRX_ERROR_SET (aResult, "Failed to get socket option.");
+	}
+	/*----------------------------------------------------------------*/
+
+	return aResult;
 }
 
@@ -84,4 +141,5 @@
 {
 	int	aResult = 0;
+
 	/*----------------------------------------------------------------*/
 	aResult = setsockopt (mSocket,
@@ -162,5 +220,5 @@
 #endif
 
-	mInitialized = true;
+	gInitialized = true;
 
 	/*----------------------------------------------------------------*/
@@ -183,5 +241,5 @@
 	/*----------------------------------------------------------------*/
 	/*----------------------------------------------------------------*/
-	return mInitialized;
+	return gInitialized;
 }
 
@@ -206,4 +264,5 @@
 {
 	int aResult = -1;
+	int aReuseAddress = 1;
 
 	/*----------------------------------------------------------------*/
@@ -219,5 +278,5 @@
 	{
 		aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
-		CRX_ERROR_SET (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
+		CRX_ERROR_SET (aResult, "Failed to create socket(%d).", CRX_SYSTEM_ERROR ());
 		return aResult;
 	}
@@ -225,5 +284,14 @@
 	mSocket = aResult;
 
-	/*----------------------------------------------------------------*/
+	aResult = SetOption (SO_REUSEADDR,
+						 &aReuseAddress,
+						 sizeof (aReuseAddress));
+	if (aResult < 0)
+	{
+		aResult = ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT;
+		CRX_ERROR_SET (aResult, "Failed to set socket option (reuseaddr).");
+	}
+	/*----------------------------------------------------------------*/
+
 	return aResult;
 }
@@ -243,5 +311,5 @@
 	{
 		aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
-		CRX_ERROR_SET (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
+		CRX_ERROR_SET (aResult, "Failed to create socket(%d).", CRX_SYSTEM_ERROR ());
 		return aResult;
 	}
@@ -267,6 +335,6 @@
 	aLinger.l_linger = 0;
 	aResult = SetOption (SO_LINGER,
-						  &aLinger,
-						  sizeof (aLinger));
+						 &aLinger,
+						 sizeof (aLinger));
 	if (aResult < 0)
 	{
@@ -279,5 +347,5 @@
 	{
 		aResult = ERROR_TCPSOCKET_FAILED_TO_SET_TIMEOUT;
-		CRX_ERROR_SET (aResult, "Failed to set timeout (%d).", GetSystemErrorCode ());
+		CRX_ERROR_SET (aResult, "Failed to set timeout (%d).", CRX_SYSTEM_ERROR ());
 		return aResult;
 	}
@@ -287,5 +355,5 @@
 	{
 		aResult = ERROR_TCPSOCKET_FAILED_TO_CONNECT;
-		CRX_ERROR_SET (aResult, "Failed to connect (%d).", GetSystemErrorCode ());
+		CRX_ERROR_SET (aResult, "Failed to connect (%d).", CRX_SYSTEM_ERROR ());
 	}
 	/*----------------------------------------------------------------*/
@@ -301,6 +369,4 @@
 	int aResult = -1;
 
-	int aReuseAddress = 1;
-
 	/*----------------------------------------------------------------*/
 	aResult = CreateSocket ();
@@ -308,15 +374,6 @@
 	{
 		aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
-		CRX_ERROR_SET (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
-		return aResult;
-	}
-
-	aResult = SetOption (SO_REUSEADDR,
-						 &aReuseAddress,
-						 sizeof (aReuseAddress));
-	if (aResult < 0)
-	{
-		aResult = ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT;
-		CRX_ERROR_SET (aResult, "Failed to set socket option (reuseaddr).");
+		CRX_ERROR_SET (aResult, "Failed to create socket(%d).", CRX_SYSTEM_ERROR ());
+		return aResult;
 	}
 
@@ -329,5 +386,5 @@
 	{
 		aResult = ERROR_TCPSOCKET_FAILED_TO_BIND;
-		CRX_ERROR_SET (aResult, "Failed to bind(%d).", GetSystemErrorCode ());
+		CRX_ERROR_SET (aResult, "Failed to bind(%d).", CRX_SYSTEM_ERROR ());
 		return aResult;
 	}
@@ -337,5 +394,5 @@
 	{
 		aResult = ERROR_TCPSOCKET_FAILED_TO_LISTEN;
-		CRX_ERROR_SET (aResult, "Failed to listen(%d).", GetSystemErrorCode ());
+		CRX_ERROR_SET (aResult, "Failed to listen(%d).", CRX_SYSTEM_ERROR ());
 		return aResult;
 	}
@@ -346,6 +403,6 @@
 		memcpy ((void *)aAddress, (void *)&mAddress, sizeof (struct sockaddr_in));
 	}
-
-	/*----------------------------------------------------------------*/
+	/*----------------------------------------------------------------*/
+
 	return aResult;
 }
@@ -359,5 +416,4 @@
 	struct sockaddr_in	aAddress;
 	socklen_t			aLength = sizeof (aAddress);
-
 	/*----------------------------------------------------------------*/
 	if (!IsReady ())
@@ -368,5 +424,5 @@
 	{
 		aResult = ERROR_TCPSOCKET_FAILED_TO_ACCEPT;
-		CRX_ERROR_SET (aResult, "Failed to accept(%d).", GetSystemErrorCode ());
+		CRX_ERROR_SET (aResult, "Failed to accept(%d).", CRX_SYSTEM_ERROR ());
 		return aResult;
 	}
@@ -380,6 +436,6 @@
 	if (aAddressLength != NULL)
 		*aAddressLength = aLength;
-
-	/*----------------------------------------------------------------*/
+	/*----------------------------------------------------------------*/
+
 	return aResult;
 }
@@ -399,9 +455,8 @@
 	{
 		aResult = ERROR_TCPSOCKET_FAILED_TO_SEND;
-		CRX_ERROR_SET (aResult, "Failed to send(%d).", GetSystemErrorCode ());
-		return aResult;
-	}
-
-	/*----------------------------------------------------------------*/
+		CRX_ERROR_SET (aResult, "Failed to send(%d).", CRX_SYSTEM_ERROR ());
+	}
+	/*----------------------------------------------------------------*/
+
 	return aResult;
 }
@@ -421,9 +476,8 @@
 	{
 		aResult = ERROR_TCPSOCKET_FAILED_TO_RECEIVE;
-		CRX_ERROR_SET (aResult, "Failed to receive(%d).", GetSystemErrorCode ());
-		return aResult;
-	}
-
-	/*----------------------------------------------------------------*/
-	return aResult;
-}
+		CRX_ERROR_SET (aResult, "Failed to receive(%d).", CRX_SYSTEM_ERROR ());
+	}
+	/*----------------------------------------------------------------*/
+
+	return aResult;
+}
