diff --git a/src/xpdev/sockwrap.c b/src/xpdev/sockwrap.c
index d372e2b89c40682c77b8a8e4b5da5dcd7fd32e8e..08b70e2f79b29a8e6abff3b9bf774b230660756c 100644
--- a/src/xpdev/sockwrap.c
+++ b/src/xpdev/sockwrap.c
@@ -87,8 +87,13 @@ int sendfilesocket(int sock, int file, long *offset, long count)
 		count-=tell(file);		/* don't try to read beyond EOF */
 	}
 
+	if(count<0) {
+		errno=EINVAL;
+		return(-1);
+	}
+
 	total=0;
-	while(total!=count) {
+	while(total<count) {
 		rd=read(file,buf,sizeof(buf));
 		if(rd==-1)
 			return(-1);
@@ -113,9 +118,6 @@ int sendfilesocket(int sock, int file, long *offset, long count)
 	if(offset!=NULL)
 		(*offset)+=total;
 
-	if(wr<1)
-		return(wr);
-
 	return(total);
 #endif
 }