diff --git a/md5.c b/md5.c
index f132ffb3a26f4f7ac72ff0421462e36815d5c3a5..cbd9312a9372efeb9cfeaf5943b3e99f7380e677 100755
--- a/md5.c
+++ b/md5.c
@@ -1,6 +1,12 @@
 /* MD5 checksum routines used for authentication.  Not covered by GPL, but
    in the public domain as per the copyright below */
-#include <endian.h>
+#if defined( __FreeBSD__ ) || defined( __OpenBSD__ )
+#  include <sys/endian.h>
+#elif defined( BSD ) && ( BSD >= 199103 )
+#  include <machine/endian.h>
+#else
+#  include <endian.h>
+#endif
 # if __BYTE_ORDER == __BIG_ENDIAN || BYTE_ORDER == BIG_ENDIAN
 #  define HIGHFIRST 1
 # endif