/** \brief peek the given number of bytes 
 *
 * Read size bytes into the buffer. Wait until size Bytes are available
 * On Dgram sockets data maight be lossed, if size is smaller then the
 * incomming package. This situation will not be treated as an error.
 * The read bytes will not be removed from the in buffer. A call to
 * recv after peek will result in the same data.
 *
 * \param buf   Pointer to the data buffer.
 * \param size  Maximum count of bytes to read.
 * \param from  Will be overwritten with the address of the sender
 *
 * \see recv Socket::recv
 */
int DgramSocket::peekFrom(void *buf,int size,Address &from)
{
    int len;
    SocketLenT addrLen=from.getSockAddrSize();

    len=recvfrom(_sd,
                 (char*)buf,
                 size,
                 MSG_PEEK,
                 from.getSockAddr(),
                 &addrLen);
    if(len==-1)
    {
#if defined WIN32
        if(getError()==WSAECONNRESET)
        {
            throw SocketConnReset("recvfrom");
        }
        if(getError()==WSAEMSGSIZE)
        {
            len=size;
        }
        else
#endif
        throw SocketError("recvfrom()");
    }
    return len;
}
/** \brief Leave a multicast group
 *
 * Don't receive messages from the given group.
 *
 * \param group   Multicast group 
 * \param interf  Which network interface to use. (Default AnyAddress)
 */
void DgramSocket::leave(const Address &group,const Address &interf)
{
    struct ip_mreq joinAddr;
    int rc;

    // group to join
    joinAddr.imr_multiaddr.s_addr
    = ((sockaddr_in*)group.getSockAddr())->sin_addr.s_addr;
    // interface that joins. (equal to bind address)
    joinAddr.imr_interface
    = ((sockaddr_in*)interf.getSockAddr())->sin_addr;
    rc=setsockopt(_sd,
                  IPPROTO_IP,
                  IP_DROP_MEMBERSHIP,
                  (SocketOptT*)&joinAddr,
                  sizeof(joinAddr));
    if(rc < 0)
    {
        throw SocketError("setsockopt(IPPROTO_IP,IP_DROP_MEMBERSHIP)");
    }
}
/** \brief Write the given number of bytes to the socket
 *
 * Write size bytes to the socket. This method maight block, if the
 * output buffer is full.
 *
 * \param buf   Pointer to the data buffer.
 * \param size  number of bytes to write
 * \param to    destination address
 *
 * \see recv Socket::send
 */
int DgramSocket::sendTo(const void *buf,int size,const Address &to)
{
    int len;

    // send Request
    len=sendto(_sd,
               (const char*)buf,size,
               0,
               to.getSockAddr(),
               to.getSockAddrSize());
    if(len == -1)
    {
        throw SocketError("sendto()");
    }
    return len;
}