diff --git a/cdr/Makefile b/cdr/Makefile
index f213caf1d0ac587c1f2ced296a4383caedf57373..e3db4ba228809dd0e0994714c3678031453342f3 100755
--- a/cdr/Makefile
+++ b/cdr/Makefile
@@ -40,6 +40,15 @@ endif
 # FreeTDS stuff...
 #
 ifneq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/include/tds.h)$(wildcard $(CROSS_COMPILE_TARGET)/usr/local/include/tds.h),)
+  ifeq ($(shell grep -s TDS_VERSION_NO $(CROSS_COMPILE_TARGET)/usr/include/tdsver.h $(CROSS_COMPILE_TARGET)/usr/local/include/tdsver.h | grep -c 0.63),1)
+    CFLAGS += -DFREETDS_0_63
+  else
+	ifeq ($(shell grep -s TDS_VERSION_NO $(CROSS_COMPILE_TARGET)/usr/include/tdsver.h $(CROSS_COMPILE_TARGET)/usr/local/include/tdsver.h | grep -c 0.62),1)
+      CFLAGS += -DFREETDS_0_62
+    else
+      CFLAGS += -DFREETDS_PRE_0_62
+    endif
+  endif
   MODS+=cdr_tds.so
 endif
 
diff --git a/cdr/cdr_tds.c b/cdr/cdr_tds.c
index 55a0053e051f40f06ac8cb2f33f627956244b57e..06cfe5973a7c946c362c1e2fb709fe5d11c651d0 100755
--- a/cdr/cdr_tds.c
+++ b/cdr/cdr_tds.c
@@ -77,8 +77,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/logger.h"
 
-#if !defined(TDS_INT_EXIT) 
-#define TDS_PRE_0_62
+#ifdef FREETDS_PRE_0_62
 #warning "You have older TDS, you should upgrade!"
 #endif
 
@@ -111,7 +110,7 @@ static int tds_log(struct ast_cdr *cdr)
 	char *accountcode, *src, *dst, *dcontext, *clid, *channel, *dstchannel, *lastapp, *lastdata, *uniqueid;
 	int res = 0;
 	int retried = 0;
-#ifdef TDS_PRE_0_62
+#ifdef FREETDS_PRE_0_62
 	TDS_INT result_type;
 #endif
 
@@ -205,7 +204,7 @@ static int tds_log(struct ast_cdr *cdr)
 			retried = 1;	/* note that we have now tried */
 		}
 
-#ifdef TDS_PRE_0_62
+#ifdef FREETDS_PRE_0_62
 		if (!connected || (tds_submit_query(tds, sqlcmd) != TDS_SUCCEED) || (tds_process_simple_query(tds, &result_type) != TDS_SUCCEED || result_type != TDS_CMD_SUCCEED))
 #else
 		if (!connected || (tds_submit_query(tds, sqlcmd) != TDS_SUCCEED) || (tds_process_simple_query(tds) != TDS_SUCCEED))
@@ -434,7 +433,11 @@ static int mssql_disconnect(void)
 
 static int mssql_connect(void)
 {
+#ifdef FREETDS_0_63
+	TDSCONNECTION *connection = NULL;
+#else
 	TDSCONNECTINFO *connection = NULL;
+#endif
 	char query[128];
 
 	/* Connect to M$SQL Server */
@@ -449,7 +452,7 @@ static int mssql_connect(void)
 	tds_set_passwd(login, password);
 	tds_set_app(login, "TSQL");
 	tds_set_library(login, "TDS-Library");
-#ifndef TDS_PRE_0_62
+#ifndef FREETDS_PRE_0_62
 	tds_set_client_charset(login, charset);
 #endif
 	tds_set_language(login, language);
@@ -479,15 +482,23 @@ static int mssql_connect(void)
 	{
 		ast_log(LOG_ERROR, "Failed to connect to MSSQL server.\n");
 		tds = NULL;	/* freed by tds_connect() on error */
+#ifdef FREETDS_0_63
+		tds_free_connection(connection);
+#else
 		tds_free_connect(connection);
+#endif
 		connection = NULL;
 		goto connect_fail;
 	}
+#ifdef FREETDS_0_63
+	tds_free_connection(connection);
+#else
 	tds_free_connect(connection);
+#endif
 	connection = NULL;
 
 	sprintf(query, "USE %s", dbname);
-#ifdef TDS_PRE_0_62
+#ifdef FREETDS_PRE_0_62
 	if ((tds_submit_query(tds, query) != TDS_SUCCEED) || (tds_process_simple_query(tds, &result_type) != TDS_SUCCEED || result_type != TDS_CMD_SUCCEED))
 #else
 	if ((tds_submit_query(tds, query) != TDS_SUCCEED) || (tds_process_simple_query(tds) != TDS_SUCCEED))
@@ -527,7 +538,7 @@ static int tds_load_module(void)
 	struct ast_config *cfg;
 	struct ast_variable *var;
 	char *ptr = NULL;
-#ifdef TDS_PRE_0_62
+#ifdef FREETDS_PRE_0_62
 	TDS_INT result_type;
 #endif