diff --git a/Makefile b/Makefile
index 3e97c7ef2812ea30a817a457180c1da58cb41ad8..121c07b520c13c0bdb5ec03a28b141f763a0b37d 100755
--- a/Makefile
+++ b/Makefile
@@ -98,6 +98,9 @@ CFLAGS=-pipe  -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarati
 CFLAGS+=$(OPTIMIZE)
 CFLAGS+=$(shell if $(CC) -march=$(PROC) -S -o /dev/null -xc /dev/null >/dev/null 2>&1; then echo "-march=$(PROC)"; fi)
 CFLAGS+=$(shell if uname -m | grep -q ppc; then echo "-fsigned-char"; fi)
+ifeq (${OSARCH},FreeBSD)
+CFLAGS+=-pthread
+endif
 ifeq (${OSARCH},OpenBSD)
 CFLAGS+=-pthread
 endif
@@ -131,7 +134,18 @@ SUBDIRS=res channels pbx apps codecs formats agi cdr astman
 ifeq (${OSARCH},Linux)
 LIBS=-ldl
 endif
-LIBS+=-lpthread -lncurses -lm -lresolv  #-lnjamd
+ifeq (${OSARCH},OpenBSD)
+LIBS=-pthread
+else
+ifeq (${OSARCH},FreeBSD)
+LIBS=-pthread
+else
+endif
+endif
+LIBS+=-lncurses -lm
+ifeq (${OSARCH},Linux)
+LIBS+=-lresolv  #-lnjamd
+endif
 OBJS=io.o sched.o logger.o frame.o loader.o config.o channel.o \
 	translate.o file.o say.o pbx.o cli.o md5.o term.o \
 	ulaw.o alaw.o callerid.o fskmodem.o image.o app.o \
diff --git a/apps/app_intercom.c b/apps/app_intercom.c
index c4be328dd7a3a1dc3b15dcdf0e87e9ab4b83ce8a..4c2379ca8c76a1a26103d44303dfedb909cfec66 100755
--- a/apps/app_intercom.c
+++ b/apps/app_intercom.c
@@ -28,6 +28,8 @@
 #include <sys/time.h>
 #ifdef __linux__
 #include <linux/soundcard.h>
+#elif defined(__FreeBSD__)
+#include <machine/soundcard.h>
 #else
 #include <soundcard.h>
 #endif
diff --git a/channels/Makefile b/channels/Makefile
index 44f614e68c2b2a1ce480f6d46802201566ef8b03..82d524a7f6a524d9b18df99729e12fa9b49953b5 100755
--- a/channels/Makefile
+++ b/channels/Makefile
@@ -91,12 +91,13 @@ ringtone.h: gentone
 
 chan_oss.o: chan_oss.c  busy.h ringtone.h
 
+ifeq (${OSARCH},OpenBSD)
+chan_oss.so: chan_oss.o
+	$(CC) -shared -Xlinker -x -o $@ chan_oss.o -lossaudio
+endif
+
 chan_iax2.so: chan_iax2.o iax2-parser.o
-ifeq (${OSARCH},Linux)
 	$(CC) -shared -Xlinker -x -o $@ chan_iax2.o iax2-parser.o
-else
-	$(CC) -shared -Xlinker -x -o $@ chan_iax2.o iax2-parser.o -lossaudio
-endif
 
 chan_zap.o: $(CHANZAP)
 	$(CC) -c $(CFLAGS) -o chan_zap.o $(CHANZAP)
diff --git a/channels/chan_iax.c b/channels/chan_iax.c
index 93f7172e9e651b3f2386026d8a6ed1ec3db9a9f2..12cc5b88447467c9e9420673368d2764eca7c612 100755
--- a/channels/chan_iax.c
+++ b/channels/chan_iax.c
@@ -532,7 +532,7 @@ static void showframe(struct ast_iax_frame *f, struct ast_iax_full_hdr *fhi, int
 	(rx ? "Rx" : "Tx"),
 	retries, ntohs(fh->seqno), class, subclass);
 		fprintf(stderr,
-"   Timestamp: %05dms  Callno: %5.5d  DCall: %5.5d [%s:%d]\n",
+"   Timestamp: %05ldms  Callno: %5.5d  DCall: %5.5d [%s:%d]\n",
 	ntohl(fh->ts),
 	(short)(ntohs(fh->callno) & ~AST_FLAG_FULL), (short) ntohs(fh->dcallno),
 		inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
diff --git a/channels/chan_oss.c b/channels/chan_oss.c
index 28f20d48571c4d30c1a0cd3f25e2f9e32d1a6978..d55f8569bdd78ba377248350b05978e7365e7657 100755
--- a/channels/chan_oss.c
+++ b/channels/chan_oss.c
@@ -35,6 +35,8 @@
 #include <stdio.h>
 #ifdef __linux
 #include <linux/soundcard.h>
+#elif defined(__FreeBSD__)
+#include <machine/soundcard.h>
 #else
 #include <soundcard.h>
 #endif
diff --git a/channels/chan_skinny.c b/channels/chan_skinny.c
index 4583814a7063068dd9f9297b604ecc2fed0fc35d..ac3c4e784fd9121b8b26733d61d7f5b2f010176f 100755
--- a/channels/chan_skinny.c
+++ b/channels/chan_skinny.c
@@ -863,6 +863,7 @@ static void transmit_tone(struct skinnysession *s, int tone)
 	transmit_response(s, req);
 }
 
+#if 0
 static void transmit_selectsoftkeys(struct skinnysession *s, int instance, int callid, int softkey)
 {
 	skinny_req *req;
@@ -881,6 +882,7 @@ static void transmit_selectsoftkeys(struct skinnysession *s, int instance, int c
 	req->data.selectsoftkey.softKeySetIndex = softkey;
 	transmit_response(s, req);
 }
+#endif
 
 static void transmit_lamp_indication(struct skinnysession *s, int instance, int indication)
 {
@@ -2318,7 +2320,7 @@ static void *accept_thread(void *ignore)
 
 	for (;;) {
 		sinlen = sizeof(sin);
-		as = accept(skinnysock, &sin, &sinlen);
+		as = accept(skinnysock, (struct sockaddr *)&sin, &sinlen);
 		if (as < 0) {
 			ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
 			continue;
@@ -2393,7 +2395,7 @@ static int restart_monitor(void)
 {
 
 	/* If we're supposed to be stopped -- stay stopped */
-	if (monitor_thread == -2)
+	if (monitor_thread == (pthread_t)-2)
 		return 0;
 	if (ast_mutex_lock(&monlock)) {
 		ast_log(LOG_WARNING, "Unable to lock monitor\n");
diff --git a/channels/iax2-parser.c b/channels/iax2-parser.c
index 5b78a50c41347cc5db2a1fa114968d16c73f65b8..1cfce461acea79f9d648a0f6a1ad877a4fecb4f5 100755
--- a/channels/iax2-parser.c
+++ b/channels/iax2-parser.c
@@ -64,7 +64,7 @@ static void dump_string(char *output, int maxlen, void *value, int len)
 static void dump_int(char *output, int maxlen, void *value, int len)
 {
 	if (len == sizeof(unsigned int))
-		snprintf(output, maxlen, "%d", ntohl(*((unsigned int *)value)));
+		snprintf(output, maxlen, "%ld", ntohl(*((unsigned int *)value)));
 	else
 		snprintf(output, maxlen, "Invalid INT");
 }
@@ -287,7 +287,7 @@ snprintf(tmp, sizeof(tmp),
 	retries, fh->oseqno, fh->iseqno, class, subclass);
 	outputf(tmp);
 snprintf(tmp, sizeof(tmp), 
-"   Timestamp: %05dms  SCall: %5.5d  DCall: %5.5d [%s:%d]\n",
+"   Timestamp: %05ldms  SCall: %5.5d  DCall: %5.5d [%s:%d]\n",
 	ntohl(fh->ts),
 	ntohs(fh->scallno) & ~IAX_FLAG_FULL, ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS,
 		inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
diff --git a/codecs/Makefile b/codecs/Makefile
index ac812972cf50f6fb82f46753c7b760dcf09593b1..c6bfbe97de9e137c340e2ff5deac09eaaab17034 100755
--- a/codecs/Makefile
+++ b/codecs/Makefile
@@ -22,6 +22,7 @@ MODG723+=$(shell [ -f g723.1b/coder2.c ] && echo "codec_g723_1b.so")
 MODSPEEX=$(shell [ -f /usr/include/speex.h ] || [ -f /usr/local/include/speex.h ] && echo "codec_speex.so")
 MODILBC=$(shell [ -f ilbc/iLBC_decode.h ] && echo "codec_ilbc.so")
 CFLAGS+=-fPIC
+CFLAGS+=$(shell [ -f /usr/local/include/speex.h ] && echo "-I/usr/local/include")
 
 LIBG723=g723.1/libg723.a
 LIBG723B=g723.1b/libg723b.a
@@ -30,7 +31,8 @@ LIBGSM=$(shell if uname -m | grep -q 86; then echo gsm/lib/libgsm.a; else echo "
 LIBGSMT=$(shell if uname -m | grep -q 86; then echo gsm/lib/libgsm.a; fi)
 LIBMP3=mp3/libmp3.a
 LIBLPC10=lpc10/liblpc10.a
-LIBSPEEX=-lspeex -lm
+LIBSPEEX=$(shell [ -f /usr/local/lib/libspeex.a ] && echo "-L/usr/local/lib")
+LIBSPEEX+=-lspeex -lm
 LIBILBC=ilbc/libilbc.a
 
 CODECS+=$(MODG723) $(MODSPEEX) $(MODILBC) codec_gsm.so codec_mp3_d.so codec_lpc10.so  \
diff --git a/codecs/lpc10/lpcini.c b/codecs/lpc10/lpcini.c
index 75e9ff2afa4f52d47687320f77df7a973141feca..78cfafdd8298ef2c0befb91ffb231aff9bc53b2a 100755
--- a/codecs/lpc10/lpcini.c
+++ b/codecs/lpc10/lpcini.c
@@ -1,7 +1,10 @@
 /*
 
 $Log$
-Revision 1.16  2003/10/16 21:11:29  martinp
+Revision 1.17  2003/10/21 02:57:29  markster
+FreeBSD patch, take 2
+
+Revision 1.16  2003/10/18 00:26:43  tholo
 Revert the previous patch since it's braking compilation
 
 Revision 1.3  2003/10/16 21:11:30  martinp
@@ -37,7 +40,7 @@ extern int lpcini_(void);
 
 #include "f2c.h"
 
-#include <malloc.h>
+#include <stdlib.h>
 
 /* Common Block Declarations */
 
@@ -51,9 +54,12 @@ struct {
 /* ***************************************************************** */
 
 /* $Log$
- * Revision 1.16  2003/10/16 21:11:29  martinp
- * Revert the previous patch since it's braking compilation
+ * Revision 1.17  2003/10/21 02:57:29  markster
+ * FreeBSD patch, take 2
  *
+/* Revision 1.16  2003/10/18 00:26:43  tholo
+/* Revert the previous patch since it's braking compilation
+/*
 /* Revision 1.3  2003/10/16 21:11:30  martinp
 /* Revert the previous patch since it's braking compilation
 /*
@@ -83,9 +89,12 @@ struct {
 {
 
 /* $Log$
- * Revision 1.16  2003/10/16 21:11:29  martinp
- * Revert the previous patch since it's braking compilation
+ * Revision 1.17  2003/10/21 02:57:29  markster
+ * FreeBSD patch, take 2
  *
+/* Revision 1.16  2003/10/18 00:26:43  tholo
+/* Revert the previous patch since it's braking compilation
+/*
 /* Revision 1.3  2003/10/16 21:11:30  martinp
 /* Revert the previous patch since it's braking compilation
 /*
@@ -115,9 +124,12 @@ struct {
 /*   LPC Configuration parameters: */
 /* Frame size, Prediction order, Pitch period */
 /* $Log$
- * Revision 1.16  2003/10/16 21:11:29  martinp
- * Revert the previous patch since it's braking compilation
+ * Revision 1.17  2003/10/21 02:57:29  markster
+ * FreeBSD patch, take 2
  *
+/* Revision 1.16  2003/10/18 00:26:43  tholo
+/* Revert the previous patch since it's braking compilation
+/*
 /* Revision 1.3  2003/10/16 21:11:30  martinp
 /* Revert the previous patch since it's braking compilation
 /*
diff --git a/codecs/mp3/include/L3.h b/codecs/mp3/include/L3.h
index e920afd94d3e20347ea7fc3a3a18f0f06ba78a82..337a721fb75d114e2a1fa737b47deb32fbc0aca6 100755
--- a/codecs/mp3/include/L3.h
+++ b/codecs/mp3/include/L3.h
@@ -37,6 +37,13 @@ ____________________________________________________________________________*/
 /* #define GLOBAL_GAIN_SCALE 0 */
 
 
+#ifdef __FreeBSD__
+#ifdef __i386__
+#undef LITTLE_ENDIAN
+#define LITTLE_ENDIAN 1
+#endif
+#endif
+
 #ifdef _M_IX86
 #define LITTLE_ENDIAN 1
 #endif