Those meanings are connected. I think it was a reference to counting sheep but it describes yearning as far as connecting the title to the plot.

Also, upon further reflection I don't really agree with what the other commenter said: "There is an old trope of counting sheep to fall asleep, but that's not a dream". If you try to and manage to think in a way that causes you to become less alert, it starts to be like daydreaming, so I think this sort of falling asleep thinking is under the umbrella of dreaming.