  't Hooft construction of free energy, electric and magnetic fluxes, and of
the partition function with twisted boundary conditions, is extended to the
case of $N=4$ supersymmetric Yang-Mills theories based on arbitrary compact,
simple Lie groups. The transformation of the fluxes and the free energy under
S-duality is presented. We consider the partition function of $N=4$ for a
particular choice of boundary conditions, and compute exactly its leading
infrared divergence. We verify that this partition function obeys the
transformation laws required by S-duality. This provides independent evidence
in favor of S-duality in $N=4$ theories.
