Q:

in what stage source port is assigned?

I opened a socket with 
sockfd = socket(AF_INET, SOCK_DGRAM, 0);

then send packet with
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr, 
  (char *)&serveraddr.sin_addr.s_addr, server->h_length);
serveraddr.sin_port = htons(portno);

sendto(sockfd, buf, strlen(buf), 0, &serveraddr, serverlen);

In what stage the source port of the socket is assigned ? in socket() or in sendto() ?
Does the source port keep unchanged in all calls of sendto() function ?

A:

sendto() perform an implicit bind if you do not call bind() explicitly.

