+ if( len != Length ) {
+ perror("SendData");
+ exit(-1);
+ }
+}
+
+int ReadData(void *Dest, int MaxLength, int Timeout)
+{
+ int ret;
+ fd_set fds;
+ struct timeval tv;
+ struct timeval *timeoutPtr;
+
+ FD_ZERO(&fds);
+ FD_SET(gSocket, &fds);
+
+ if( Timeout ) {
+ tv.tv_sec = Timeout;
+ tv.tv_usec = 0;
+ timeoutPtr = &tv;
+ }
+ else {
+ timeoutPtr = NULL;
+ }
+
+ ret = select(gSocket+1, &fds, NULL, NULL, timeoutPtr);
+ if( ret == -1 ) {
+ perror("ReadData - select");
+ exit(-1);
+ }
+
+ if( !ret ) {
+ printf("Timeout reading from socket\n");
+ return 0; // Timeout
+ }
+
+ #if USE_TCP
+ ret = recv(gSocket, Dest, MaxLength, 0);
+ #else
+ ret = recvfrom(gSocket, Dest, MaxLength, 0, NULL, 0);
+ #endif
+
+ if( ret < 0 ) {
+ perror("ReadData");
+ exit(-1);
+ }
+
+ printf("%i bytes read from socket\n", ret);
+
+ return ret;