Diffie-Hellman server/client generated secret size So I have implemented a client and a server performing Diffie-Hellmann, however sometimes the symmetric key is not valid? 

Example: 


Prime: 23 
Primitive root: 5 
client secret: 14 
server secret: 16


This results in a faulty generated symmetric key.


Client shared key:      $${5^{14}\mod 23}=13$$
Server shared key:      $${5^{16}\mod 23}=3$$
Client "symmetric key": $${3^{14}\mod 23}=4$$
Server "symmetric key": $${13^{16}\mod 23}=8$$ 


So we can conclude that the provided arguments resulted in a faulty Diffie-Hellman since $4\ne8$.

What is the rule for the pseudorandom generated secret keys of the server and the client?

Javascript is used for the calculations, e.g. 13**16%23.
The issue is plain and simply: JavaScript sucks.

In this particular case the issue is that JS has no integer type, so everything is stored as a double. In particular the last calculation $13^{16}$ would yield a 60-bit integer which JS computes as ordered. However double only has 52 bits for the mantissa and so can't possibly contain the full value. So JS rounds. Then it takes the rounded number and applies the $\bmod 23$ reduction yielding some unrelated value to the one you intended to compute.